THINK FIRST·CODE LATER

← Java Programming
Chapter 3 · Week 3

Java Basics: Class Structure, Variables, and Data Types

Answered 0/38 Correct 0
Sign in to save progress across devices
Q1

An object is an instance of a ________.

Q2

Which line correctly declares and initialises a variable holding a whole number of students?

Q3

What is the default value of an int instance variable that the programmer does not initialise?

Q4

Analyse the following code. What is the output?

public class Test {
    int x;
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.x);
    }
}
Q5

What is the output of the following program?

public class Question4 {
    public static void main(String args[]) {
        String tree = "pine";
        int count = 0;
        if (tree.equals("pine")) {
            int height = 55;
            count = count + 1;
        }
        System.out.print(height + count);
    }
}
Q6

Which of the following is not a primitive type in Java?

Q7

How many bits does a Java int occupy?

Q8

Which type would you choose for a population count of 8 billion?

Q9

Which declaration does not compile?

Q10

Which declaration does not compile?

Q11

What is the type of the literal 7 in double x = 7;?

Q12

Which statement about boolean is correct?

Q13

Which of the following correctly declares a character variable?

Q14

What is the effect of the keyword final on a variable?

Q15

Which of these follows the standard Java naming convention for a constant?

Q16

Which of the following compiles?

Q17

What is wrong with the following code?

public static void main(String[] args) {
    int total;
    System.out.println(total);
}
Q18

What value of k does the second println statement print?

public class Foo {
    static int i = 0;
    static int j = 0;
    public static void main(String[] args) {
        int i = 2;
        int k = 3;
        {
            int j = 3;
            System.out.println("i + j is " + i + j);
        }
        k = i + j;
        System.out.println("k is " + k);   // second println
        System.out.println("j is " + j);
    }
}
Q19

In the program of the previous question, what does the first println print?

Q20

Which statement about variable scope is true?

Q21

Which statement declares three int variables in one line, initialising only the first?

Q22

Which of the following is a valid variable declaration?

Q23

What does the following statement print?

System.out.println("Total: " + 5 + 3);
Q24

And this one?

System.out.println("Total: " + (5 + 3));
Q25

What is stored in c after char c = 66;?

Q26

Which line does not compile?

int a = 5;        // L1
double b = a;     // L2
int c = b;        // L3
char d = 'x';     // L4
Q27

A variable of type String stores:

Q28

Which of the following is the correct way to write a comment that spans three lines?

Q29

What is the value of x after int x = 1_000_000;?

Q30

Which of the following statements about System.out.println is correct?

Q31

An instance variable differs from a local variable because:

Q32

What is printed?

public class Counter {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i < 3; i++) {
            int inner = 0;
            inner++;
            count++;
        }
        System.out.print(count);
    }
}
Q33

In the previous question, what would System.out.print(inner) after the loop cause?

Q34

Which pair of values could be stored in a byte?

Q35 Short answer

Explain the difference between declaring, initialising and assigning a variable, giving one line of Java for each.

Q36 Short answer

Why does Java require an explicit cast for int x = someDouble; but not for double y = someInt;?

Q37 Short answer

Give two reasons why a programmer should write final double VAT_RATE = 0.06; instead of writing 0.06 in five different places.

Q38 Programming

Write a class CircleInfo whose main method declares a final double PI = 3.14159, a radius of 7.5, and prints the diameter, circumference and area on three labelled lines using println.