1Z0-811 · Question #29
Given the code fragment: int a = 10; int b = 20; int c = 30; System.out.println (a++ > 10 || ++b <= 21); System.out.println (a > 10 && ++b <= 22); System.out.println (a <= 11 && b == 22)…
The correct answer is C. true true true true. Option C (true true true true) is correct because tracing through each line with careful attention to post/pre-increment and short-circuit evaluation yields all true results. Let me walk through it: Line 1: a++ yields 10 (post-increment evaluates before incrementing, so a…
Question
Options
- Afalse true false false
- Btrue false false false
- Ctrue true true true
- Dtrue true true false
How the community answered
(43 responses)- A19% (8)
- B9% (4)
- C67% (29)
- D5% (2)
Explanation
Option C (true true true true) is correct because tracing through each line with careful attention to post/pre-increment and short-circuit evaluation yields all true results. Let me walk through it:
Line 1: a++ yields 10 (post-increment evaluates before incrementing, so a becomes 11 after), making 10 > 10 false - but || is not short-circuited, so ++b fires, making b = 21, and 21 <= 21 is true. → false || true = true
Line 2: a is now 11, so 11 > 10 is true; && does not short-circuit, ++b fires making b = 22, and 22 <= 22 is true. → true && true = true
Line 3: 11 <= 11 is true, 22 == 22 is true. → true && true = true
Line 4: && binds tighter than ||, so it parses as (c++ == 31 && a++ == 11) || (b++ == 22). c++ yields 30 (c was 30), so 30 == 31 is false - && short-circuits, skipping a++. The left group is false, so || must evaluate b++ == 22: b is still 22, so 22 == 22 is true. → false || true = true
Distractors A and B fail because they misread lines 1–3 (often by treating a++ as 11 in the first comparison). Option D fails on line 4 - test-takers who skip short-circuit analysis assume a++ == 11 is evaluated and somehow flips the result, but it isn't reached at all.
Memory tip: Think "POST = use it, THEN boost it" (post-increment) and "PRE = boost it, THEN use it" (pre-increment). For short-circuits: && stops on the first false, || stops on the first true - any side effects (like increments) on the skipped side never happen.
Topics
Community Discussion
No community discussion yet for this question.