1Z0-819 · Question #4
Given this code fragment: int x = 0; do { x++; if (x == 1) { continue; } System.out.println(x); } while (x < 1); What is the result?
The correct answer is D. The program prints nothing. D is correct because the loop body executes exactly once: x increments from 0 to 1, the if (x == 1) condition is true so continue fires - skipping System.out.println(x) - and then the while (x < 1) condition evaluates to false (since 1 < 1 is false), ending the loop with no…
Question
Options
- A01
- B0
- C1
- DThe program prints nothing.
- EIt prints 1 in the infinite loop.
How the community answered
(37 responses)- A11% (4)
- B3% (1)
- C5% (2)
- D78% (29)
- E3% (1)
Explanation
D is correct because the loop body executes exactly once: x increments from 0 to 1, the if (x == 1) condition is true so continue fires - skipping System.out.println(x) - and then the while (x < 1) condition evaluates to false (since 1 < 1 is false), ending the loop with no output ever produced.
Why the distractors fail: C ("1") is the most tempting trap - x does equal 1, but continue skips past the print statement before it can execute. A ("01") and B ("0") are impossible because x++ runs first in every iteration, meaning x is never 0 when println could fire. E (infinite loop) misreads the loop direction - once x reaches 1, the while condition x < 1 immediately becomes false and exits.
Memory tip: In a do-while, continue doesn't restart the body - it jumps directly to the condition check. Always ask yourself: "What is x's value when the condition is evaluated?" Here, continue lands you at while (x < 1) with x = 1, which kills the loop before a second iteration can ever print anything.
Topics
Community Discussion
No community discussion yet for this question.