LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 2

Error: duplicate case labels Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

The program below does not compile. Which statement correctly describes the problem?

Main.java
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links