Question 7
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
public class Main {
public static void main(String[] args) {
search:
for (int i = 0; i < 3; i++) {
System.out.println(i);
}
for (int j = 0; j < 3; j++) {
if (j == 1) break search;
}
}
}
- A Labels must be uppercase
- B The label search belongs to the first loop, which does not enclose the break; a labelled break can only target an enclosing statement Correct
- C j must start at 1
- D break cannot have a label
Correct Answer: Option B
Labels are not goto targets – they only name the statement they precede.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!