1Z0-808 · Question #46
public static void main(String[] args) { String[][] arr = {{"A", "B", "C"}, {"D", "E"}}; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { System.out.print(arr[i][j] +…
The correct answer is C. D. The stated correct answer (C) does not match the actual behavior of this code. Tracing through the logic: when i=0, the inner loop prints A, then prints B and hits break, exiting only the inner loop. The outer continue is redundant (it's the last statement) and simply advances…
Question
Options
- AABC
- BABCDE
- CD
- DCompilation fails.
How the community answered
(51 responses)- A6% (3)
- B22% (11)
- C65% (33)
- D8% (4)
Explanation
The stated correct answer (C) does not match the actual behavior of this code. Tracing through the logic: when i=0, the inner loop prints A, then prints B and hits break, exiting only the inner loop. The outer continue is redundant (it's the last statement) and simply advances to i=1. When i=1, the inner loop prints D, then E, with no break triggered since neither equals "B". Total output: A B D E (with spaces from print).
None of the four choices correctly match this output, which suggests the question has an error - either in the code, the answer choices, or the marked answer.
Why each distractor is also wrong:
- A (ABC):
Cis never printed becausebreakexits the inner loop afterB. - B (ABCDE):
breakpreventsCfrom printing, so this overstates the output. - D (Compilation fails): The code is syntactically valid Java and compiles fine.
Memory tip: break only exits the innermost enclosing loop or switch - never an outer loop unless a label is used. A continue at the end of a loop body is always a no-op (the loop would continue anyway). When tracing nested loops, always ask: which loop does this break/continue target?
If this is from an official exam or textbook, I'd recommend flagging the question - the correct output
A B D Eisn't among the choices.
Topics
Community Discussion
No community discussion yet for this question.