LearnFrenzy
Sign in Take a free test

Control Flow Statements · Find the Error · Question 3

Error: a variable used as a case label Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 3 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 limit = 10;
        int value = 10;
        switch (value) {
            case limit: System.out.println("at limit"); break;
            default: System.out.println("other");
        }
    }
}
  • A value must be long
  • B Case labels must be compile-time constants; limit is a non-final variable (making it final int limit = 10 would fix it) Correct
  • C default must come first
  • D switch cannot use int
Correct Answer: Option B

For changing values, use if/else.

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