LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 1

A switch without break statements Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        int x = 2;
        switch (x) {
            case 1: System.out.print("one ");
            case 2: System.out.print("two ");
            case 3: System.out.print("three ");
            default: System.out.print("other ");
        }
        System.out.println();
    }
}
  • A two three other Correct
  • B two
  • C two other
  • D one two three other
Correct Answer: Option A

Execution starts at case 2 and falls through every following label, including default.

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