nerdexam
Oracle

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…

Using Loop Constructs

Question

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] + " "); if (arr[i][j].equals("B")) { break; } } continue; } } What is the result?

Options

  • AABC
  • BABCDE
  • CD
  • DCompilation fails.

How the community answered

(51 responses)
  • A
    6% (3)
  • B
    22% (11)
  • C
    65% (33)
  • D
    8% (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): C is never printed because break exits the inner loop after B.
  • B (ABCDE): break prevents C from 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 E isn't among the choices.

Topics

#nested loops#break statement#2D arrays#continue statement

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice