nerdexam
Oracle

1Z0-811 · Question #43

Given the code fragment: int value = 10; int a = ++value; int b = value; int c = value++; if (a <= b && a <= c) { if (b <= c) { a = ++b; } else { a = ++c; } } System.out.println(a); What is the…

The correct answer is C. 12. Tracing through the code step by step: ++value pre-increments value to 11 before assignment, so a = 11; b = value copies the current value, so b = 11; value++ post-increments, so c = 11 and value becomes 12. The outer condition a <= b && a <= c evaluates to 11 <= 11 && 11 <= 11…

Data Types and Operators

Question

Given the code fragment: int value = 10; int a = ++value; int b = value; int c = value++; if (a <= b && a <= c) { if (b <= c) { a = ++b; } else { a = ++c; } } System.out.println(a); What is the result?

Options

  • A10
  • B11
  • C12
  • D13

How the community answered

(28 responses)
  • A
    4% (1)
  • B
    14% (4)
  • C
    75% (21)
  • D
    7% (2)

Explanation

Tracing through the code step by step: ++value pre-increments value to 11 before assignment, so a = 11; b = value copies the current value, so b = 11; value++ post-increments, so c = 11 and value becomes 12. The outer condition a <= b && a <= c evaluates to 11 <= 11 && 11 <= 11 - both true - so we enter it, and the inner condition b <= c is 11 <= 11 (also true), executing a = ++b, which pre-increments b to 12 and assigns it to a, giving a final printed value of 12.

Why the distractors are wrong:

  • A (10) is the original value of value before any operations - a trap for ignoring all the increments entirely.
  • B (11) is the value of a, b, and c after initialization - a trap for stopping before the if block executes.
  • D (13) would require two increment operations to be applied to a, which never happens - the else branch is never reached.

Memory tip: For pre/post-increment questions, label each variable's value after every single line before evaluating any condition - rushing to the if without tracking state is exactly what these questions are designed to exploit.

Topics

#pre-increment vs post-increment#operator semantics#variable assignment#control flow

Community Discussion

No community discussion yet for this question.

Full 1Z0-811 Practice