Question 2
Back to Exercise
The program below does not compile. Which statement correctly describes the problem?
public class Main {
public static void main(String[] args) {
int code = 2;
switch (code) {
case 1: System.out.println("one"); break;
case 2: System.out.println("two"); break;
case 2: System.out.println("again two"); break;
}
}
}
- A switch needs default
- B case 2 appears twice; every case label must be unique Correct
- C code must be final
- D break is not allowed in switch
Correct Answer: Option B
Combine the bodies or use different values.
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!