LearnFrenzy
Sign in Take a free test

Control Flow Statements · Predict the Output · Question 18

A classic switch on an enum with fall-through grouping Answer, explanation & discussion

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

What is the output of the following Java program?

Main.java
public class Main {
    enum Size { S, M, L, XL }

    public static void main(String[] args) {
        int price = 0;
        for (Size s : Size.values()) {
            switch (s) {
                case S:
                case M:
                    price += 300;
                    break;
                case L:
                    price += 100;            // no break: L also gets the XL surcharge
                case XL:
                    price += 400;
                    break;
            }
        }
        System.out.println(price);
    }
}
  • A 1500 Correct
  • B 1100
  • C 1400
  • D 1000
Correct Answer: Option A

S and M share one body (600). L adds 100 and falls into XL's 400 (500); XL adds 400. Total 1500.

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