Question 2
Back to Exercise
What is the output of the following Java program?
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!