Using Operators and Decision Constructs practice questions

From OCA Java SE 7 (1Z0-803) · 25 questions on this topic

Using Operators and Decision Constructs practice questions from OCA Java SE 7 (1Z0-803). This pack has 25 questions tagged Using Operators and Decision Constructs, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Using Operators and Decision Constructs

  1. Question 1

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { int x = 0; boolean r = (x++ > 0) | (x++ > 0); System.out.println(x + " " + r); } } ```

    1. A. 2 trueCorrect answer

      Single | never short-circuits, so both operands run: 0 > 0 false (x→1) then 1 > 0 true (x→2), and false | true is true (JLS 7 §15.22.2).

    2. B. 1 false

      This treats | as if it short-circuited like ||, but single | always evaluates both operands, so x reaches 2 and the true right operand makes the result true.

    3. C. 2 false

      The count of 2 is right, but the boolean is wrong: the right operand 1 > 0 is true, so false | true evaluates to true.

    4. D. 1 true

      This assumes only one increment runs, but | never short-circuits, so both x++ operands execute and x ends at 2, not 1.

    Explanation

    Single | is the NON-short-circuit OR: both operands are always evaluated. Left: 0 > 0 false (x→1). Right: 1 > 0 true (x→2). false | true is true. Contrast with ||, which would also have evaluated both here (left was false) — the visible difference between | and || only appears when the left side is true.

  2. Question 2

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { System.out.println(2 + 3 * 2 + "" + (2 + 3) * 2); } } ```

    1. A. 1010

      This assumes the left side also totals 10, but 2 + 3 * 2 evaluates to 8 because multiplication binds tighter than addition.

    2. B. 820

      The left side 8 is right, but the right side is mis-added: * still binds tighter than +, so (2 + 3) * 2 is 10, not 20, giving "810".

    3. C. 16

      This treats the whole expression as numeric (8 + 8), ignoring the empty string that switches everything after it to string concatenation.

    4. D. 810Correct answer

      Left of the empty string, 2 + 3 * 2 = 8; the "" makes the rest string territory, but * still binds tighter so (2 + 3) * 2 = 10 is computed numerically, yielding "8" + "10" = "810" (JLS 7 §15.18.1).

    Explanation

    Left of the empty string: 2 + 3 * 2 = 8 (multiplication binds tighter than addition). Concatenating with "" turns everything after into string territory, but * still binds tighter than +, so (2 + 3) * 2 = 10 is computed numerically before being appended. "8" + "10" = "810".

  3. Question 3

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { char grade = 'B'; switch (grade) { case 'A': System.out.println("excellent"); break; case 'B': System.out.println("good"); break; default: System.out.println("other"); } } } ```

    1. A. goodCorrect answer

      grade 'B' matches case 'B', printing good, and the break prevents fall-through.

    2. B. excellent

      That is the case 'A' output, but grade is 'B', so case 'B' is selected.

    3. C. other

      default runs only when no case matches; here case 'B' matches, so other is not printed.

    4. D. Compilation fails because char is not allowed in a switch

      char is a valid switch selector type, so the switch compiles.

    Explanation

    char is one of the allowed switch selector types (it's int-compatible). 'B' matches its case, prints "good", and the break prevents fall-through.

  4. Question 4

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { int a = 0; boolean r = (a++ > 0) && (a++ > 0); System.out.println(a + " " + r); } } ```

    1. A. 2 false

      This assumes both a++ operands are evaluated so a reaches 2, but && short-circuits after the false left operand, so the right a++ never runs and a stays 1.

    2. B. 2 true

      Both parts are wrong: the left comparison 0 > 0 is false so the result cannot be true, and short-circuiting means the second increment never happens, so a is not 2.

    3. C. 1 true

      The count of 1 is right, but the boolean is wrong: the only operand evaluated is 0 > 0, which is false, so the && result is false, not true.

    4. D. 1 falseCorrect answer

      The left operand a++ > 0 is 0 > 0 → false (a becomes 1), and && short-circuits so the right operand never runs; a is 1 and r is false (JLS 7 §15.23).

    Explanation

    The left operand a++ > 0 compares the old value 0 > 0 → false (a becomes 1). && short-circuits: since the left side is false, the right operand is never evaluated, so a is not incremented again. Result: a is 1, r is false.

  5. Question 5

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { long key = 2; switch (key) { case 2: System.out.println("two"); break; default: System.out.println("other"); } } } ```

    1. A. two

      This assumes a long selector is valid and matches case 2, but long is not a permitted switch type, so the switch never runs and nothing prints.

    2. B. Compilation failsCorrect answer

      A switch selector may be byte, short, char, int (or wrappers), an enum, or String; long is not allowed, so switching on a long is a compile error (JLS 7 §14.11).

    3. C. other

      This assumes the default branch executes, but the program does not compile because a long cannot be a switch selector, so no branch runs.

    4. D. An exception is thrown at runtime

      The illegal long selector is caught by the compiler, so the failure happens at compile time; the program never runs, so no runtime exception occurs.

    Explanation

    A switch selector may be byte, short, char, int (or their wrappers), an enum, or — new in Java 7 — String. long, float, double, and boolean are NOT allowed, so `switch (key)` on a long is a compile error. Cast it to int (or declare key as int) and it works.

  6. Question 6

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { int n = 3; final int TWO = 2; int four = 4; switch (n) { case TWO: System.out.println("two"); break; case four: System.out.println("four"); break; } } } ```

    1. A. It compiles and prints nothing

      It would print nothing for n=3 only if it compiled, but the non-final variable used as a case label prevents compilation, so this is wrong.

    2. B. four

      Even if it compiled, n=3 would not match the case whose value is 4; and it does not compile because that label is a non-constant variable.

    3. C. Compilation failsCorrect answer

      Case labels must be compile-time constants; the ordinary (non-final) variable used as a case label is rejected with "constant expression required" (JLS 7 §14.11, §15.28).

    4. D. two

      n=3 would not match the case whose value is 2 even if it compiled, and the program does not compile because of the non-constant case label.

    Explanation

    Case labels must be compile-time constants. TWO qualifies (final and initialized with a constant), but `four` is an ordinary variable — even though its value looks obvious, the compiler rejects `case four:` with "constant expression required". Make it final and it compiles (printing nothing for n=3).

  7. Question 7

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { System.out.println((-7 % 3) + " " + (7 % -3)); } } ```

    1. A. 1 -1

      This swaps the signs: the remainder sign follows the dividend, not the divisor, so -7 % 3 is -1 (not 1) and 7 % -3 is 1 (not -1).

    2. B. -1 -1

      The first value is right, but this incorrectly makes the second negative from the divisor; since the dividend 7 is positive, 7 % -3 is +1.

    3. C. -1 1Correct answer

      The remainder takes the sign of the dividend (left operand): -7 % 3 is -1 and 7 % -3 is 1, with integer division truncating toward zero (JLS 7 §15.17.3).

    4. D. 2 1

      This treats % as a mathematical modulo that yields a non-negative result, but Java's remainder truncates toward zero, so -7 % 3 is -1, not 2.

    Explanation

    The sign of the remainder follows the DIVIDEND (left operand), never the divisor: -7 % 3 is -1, and 7 % -3 is 1. The identity is (a/b)*b + a%b == a with integer division truncating toward zero.

  8. Question 8

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { int n = 1; switch (n) { case 1: System.out.println("one"); break; case 1: System.out.println("uno"); break; } } } ```

    1. A. one

      The switch never runs; two identical case 1 labels are a compile error.

    2. B. Compilation failsCorrect answer

      Every case label must be unique, so two case 1 labels are a duplicate case label compile error.

    3. C. one then uno

      The duplicate case labels prevent compilation, so nothing is printed.

    4. D. uno

      The code does not compile due to the duplicate case, so no output is produced.

    Explanation

    Every case label in a switch must be unique — two `case 1:` labels are a "duplicate case label" compile error. The compiler must be able to jump to exactly one entry point per value.

Practise all 25 Using Operators and Decision Constructs questions

OCA Java SE 7 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCA Java SE 7

Other topics in this pack

Using Operators and Decision Constructs — 1Z0-803 practice questions with explanations · TestHoop