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…
Question
Options
- A10
- B11
- C12
- D13
How the community answered
(28 responses)- A4% (1)
- B14% (4)
- C75% (21)
- D7% (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
valuebefore any operations - a trap for ignoring all the increments entirely. - B (11) is the value of
a,b, andcafter initialization - a trap for stopping before theifblock executes. - D (13) would require two increment operations to be applied to
a, which never happens - theelsebranch 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
Community Discussion
No community discussion yet for this question.