1Z0-809 · Question #133
Given the code fragment: public static void main(String[] args) { String[] arr = {"A", "B", "C", "D"}; for (int i = 0; i < arr.length; i++) { System.out.println(arr[i] + " "); if…
The correct answer is B. A B C Work done. Option B is correct because continue and break behave very differently: when arr[i] equals "C", the continue statement skips the rest of that iteration's body (including "Work done" and break), jumping immediately to the next loop iteration - so "A ", "B ", and "C " each print…
Question
Options
- AA B C D Work done
- BA B C Work done
- CA B C Work done
- DCompilation fails.
How the community answered
(21 responses)- A14% (3)
- B76% (16)
- C5% (1)
- D5% (1)
Explanation
Option B is correct because continue and break behave very differently: when arr[i] equals "C", the continue statement skips the rest of that iteration's body (including "Work done" and break), jumping immediately to the next loop iteration - so "A ", "B ", and "C " each print without triggering the break. Only once the loop moves past "C" does it reach System.out.println("Work done") followed by break, which exits the loop before "D" is ever printed.
Option A is wrong because "D" is never printed - the break terminates the loop after "Work done", so the fourth element is never reached. Option C likely differs from B in whitespace/newline formatting, since println places each output on its own line rather than a single space-separated line. Option D is wrong because the code is syntactically and semantically valid Java - there is nothing that would cause a compilation error.
Memory tip: Think of continue as "skip this lap, keep running" and break as "leave the track entirely" - once you internalize that continue only skips the rest of the current iteration (not the whole loop), break/continue output-tracing questions become straightforward.
Community Discussion
No community discussion yet for this question.