LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 2

A default label in the middle of a switch Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

What is the output of the following Java program?

Main.java
public class Main {
    public static void main(String[] args) {
        for (int x : new int[]{1, 5, 3}) {
            switch (x) {
                case 1: System.out.print("A");
                default: System.out.print("D");
                case 3: System.out.print("C"); break;
                case 4: System.out.print("F");
            }
            System.out.print(" ");
        }
        System.out.println();
    }
}
  • A ADC DC C Correct
  • B A D C
  • C ADC D C
  • D AD DCF C
Correct Answer: Option A

default can appear anywhere; it is chosen only when no case matches, and falls through like any other label.

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