Using Operators and Decision Constructs practice questions

From OCA Java SE 8 (1Z0-808) · 21 questions on this topic

Using Operators and Decision Constructs practice questions from OCA Java SE 8 (1Z0-808). This pack has 21 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 result of compiling the following program? ```java public class Main { public static void main(String[] args) { System.out.println(true > false); } } ```

    1. A. true

      This assumes booleans are ordered with true above false; Java gives booleans no ordering at all.

    2. B. Compilation failsCorrect answer

      Relational operators apply only to numeric types, so comparing booleans with > is a "bad operand types" error (JLS 8 §15.20.1).

    3. C. false

      No comparison result is produced at all — the expression is rejected while compiling.

    4. D. 1

      Booleans have no numeric value in Java, and a relational operator would yield a boolean rather than an int anyway.

    Explanation

    Relational operators (<, >, <=, >=) apply only to NUMERIC types — booleans have no ordering in Java ("bad operand types"). Only ==, !=, and the logical operators accept booleans.

  2. Question 2

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

    1. A. true

      By definition null is an instance of no type whatsoever, so instanceof against any type yields false, never true.

    2. B. Compilation fails because null has no type

      The null literal is a legal left operand for instanceof; the expression compiles fine and is simply defined to evaluate to false.

    3. C. It throws a NullPointerException

      instanceof never dereferences its operand — it just tests the reference — so a null left side produces false rather than a NullPointerException.

    4. D. falseCorrect answer

      Correct. instanceof is defined to return false whenever the left operand is null, for every type, and no dereference occurs (JLS 8 §15.20.2).

    Explanation

    The null literal is a legal left operand for instanceof (`Compilation fails because null has no type` is wrong), and the result is defined to be false for every type — null is an instance of nothing. No dereference happens, so no NPE.

  3. Question 3

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { boolean t = true; t &= false; System.out.println(t); } } ```

    1. A. true

      The compound operator really does update the variable: t = t & false is false.

    2. B. Compilation fails because &= only applies to integers

      The compound forms &=, |= and ^= work on booleans too, applying the logical operators.

    3. C. An exception is thrown at runtime

      Nothing here can throw — it is a plain boolean assignment.

    4. D. falseCorrect answer

      t &= false expands to t = t & false = false, since the compound forms apply to booleans as well as integers (JLS 8 §15.26.2, §15.22.2).

    Explanation

    The compound forms &=, |=, ^= work on booleans too (`Compilation fails because &= only applies to integers` is wrong) — t &= false is t = t & false = false. (There are no &&= or ||= operators, though.)

  4. Question 4

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

    1. A. 9 15

      Computes r as 7 + 8, missing that ++w increments w to 9 before its value is read. The pre-increment contributes 9, not 8, so r is 16.

    2. B. 9 16Correct answer

      Correct. Evaluating left to right, w++ yields 7 (w becomes 8), then ++w bumps w to 9 and yields 9, so r = 7 + 9 = 16 and w ends at 9 (JLS 8 §15.14.2, §15.15.1).

    3. C. 9 14

      Undercounts the sum, effectively using 7 + 7. The first term is the read value 7 but the second term is 9 after the pre-increment, giving 16, not 14.

    4. D. 8 15

      Reports w as 8, counting only one increment. Both w++ and ++w modify w, so it ends at 9, and the sum is 16.

    Explanation

    Left to right: w++ yields 7 (w becomes 8), then ++w bumps w to 9 and yields 9. r = 7 + 9 = 16, w ends at 9. `9 15` (7+8) misses that the pre-increment happens before its value is read.

  5. Question 5

    Which operator has the highest precedence among the following?

    1. A. * (multiplication)

      Multiplicative operators bind tighter than addition, but they sit below both unary and postfix operators on the precedence ladder, so they are not the highest here.

    2. B. + (addition)

      Additive precedence is lower than multiplicative, which is itself lower than postfix — addition is nowhere near the top of the ladder.

    3. C. = (assignment)

      This is the exact opposite: assignment has the lowest precedence of all operators and is evaluated last, after every arithmetic and postfix operation.

    4. D. ++ (post-increment)Correct answer

      Correct. Postfix operators sit at the very top of the precedence ladder, above unary, multiplicative, additive, and far above assignment (JLS 8 §15).

    Explanation

    Postfix operators sit at the very top of the precedence ladder, above unary, multiplicative (*), additive (+), and far above assignment — which is the lowest of all and evaluated last.

  6. Question 6

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { System.out.println(1 + 2 > 3 & 4 % 2 == 0); } } ```

    1. A. falseCorrect answer

      Correct. Arithmetic runs first (1+2=3, 4%2=0), then relational (3 > 3 is false, 0 == 0 is true), then the boolean & combines them: false & true is false (JLS 8 §15.22.2).

    2. B. true

      Requires both sides of the & to be true. The left side 3 > 3 is false (not strictly greater), so the AND is false regardless of the right side.

    3. C. Compilation fails

      A single & applied to two boolean operands is the legal non-short-circuit AND; the expression is well-typed and compiles.

    4. D. 3false

      Misreads the expression as string concatenation. Every operand here is numeric or boolean and the whole expression evaluates to a single boolean, so nothing like "3" is printed alongside the result.

    Explanation

    Precedence: arithmetic first (1+2=3, 4%2=0), then relational (3 > 3 is false, 0 == 0 is true), then & combines the booleans: false & true = false. The single & on booleans is the non-short-circuit AND — legal, both sides evaluated.

  7. Question 7

    What is the type of the expression `i + n` when i is an int and n is a long?

    1. A. int

      Promotion widens to the larger type, not the smaller — the long operand pulls the result up.

    2. B. double

      Nothing here is floating-point, so promotion goes no further than long.

    3. C. It depends on which operand is larger at runtime

      Promotion is decided at compile time from the declared types; runtime values are irrelevant to an expression's type.

    4. D. longCorrect answer

      Binary numeric promotion widens the smaller type to the larger at compile time — int meets long, so both become long and the result is long (JLS 8 §5.6.2).

    Explanation

    Binary numeric promotion widens the smaller TYPE to the larger one at COMPILE time — int meets long, both become long, result is long. Runtime values are irrelevant (`It depends on which operand is larger at runtime` is wrong).

  8. Question 8

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { String day = "Mon"; switch (day) { case "mon": System.out.println("start"); break; default: System.out.println("other"); } } } ```

    1. A. start

      This assumes matching ignores case; String switch uses equals, so "Mon" does not match case "mon".

    2. B. otherCorrect answer

      String switch matching uses equals, which is case-sensitive, so "Mon" misses case "mon" and default runs (JLS 8 §14.11).

    3. C. Compilation fails

      Switching on a String is legal, and this switch is well formed.

    4. D. An exception is thrown at runtime

      An unmatched value simply falls to default; nothing throws.

    Explanation

    String switch matching uses equals — which is CASE-SENSITIVE. "Mon" does not match case "mon", so default runs. There is no equalsIgnoreCase behavior in switch.

Practise all 21 Using Operators and Decision Constructs questions

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

Open OCA Java SE 8

Other topics in this pack

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