Review Questions

graphics/rq_icon.gif

3.15

Which of the following expressions evaluates to true?

Select the two correct answers.

  1. (false | true)

  2. (null != null)

  3. (4 <= 4)

  4. (!true)

  5. (true & false)

3.16

Which statements are true?

Select the two correct answers.

  1. The remainder operator % can only be used with integral operands.

  2. Identifiers in Java are case insensitive.

  3. The arithmetic operators *, /, and % have the same level of precedence.

  4. A short value ranges from -128 to +127 inclusive.

  5. (+15) is a legal expression.

3.17

Which statements are true about the lines of output printed by the following program?

public class BoolOp {
    static void op(boolean a, boolean b) {
        boolean c = a != b;
        boolean d = a ^ b;
        boolean e = c == d;
        System.out.println(e);
    }

    public static void main(String[] args) {
        op(false, false);
        op(true, false);
        op(false, true);
        op(true, true);
    }
}

Select the three correct answers.

  1. All lines printed are the same.

  2. At least one line contains false.

  3. At least one line contains true.

  4. The first line contains false.

  5. The last line contains true.

3.18

What happens during execution of the following program?

public class OperandOrder {
    public static void main(String[] args) {
        int i = 0;
        int[] a = {3,6};
        a[i] = i = 9;
        System.out.println(i + " " + a[0] + " " + a[1]);
    }
}

Select the one correct answer.

  1. Throws an exception of type ArrayIndexOutOfBoundsException

  2. Prints "9 9 6"

  3. Prints "9 0 6"

  4. Prints "9 3 6"

  5. Prints "9 3 9"

3.19

Which statements are true about the output of the following program?

public class Logic {
    public static void main(String[] args) {
        int i = 0;
        int j = 0;

        boolean t = true;
        boolean r;

        r = (t &  0<(i+=1));
        r = (t && 0<(i+=2));
        r = (t |  0<(j+=1));
        r = (t || 0<(j+=2));
        System.out.println(i + " " + j);
    }
}

Select the two correct answers.

  1. The first digit printed is 1.

  2. The first digit printed is 2.

  3. The first digit printed is 3.

  4. The second digit printed is 1.

  5. The second digit printed is 2.

  6. The second digit printed is 3.