Multiple choice technology programming languages

class Demo { public static void main(String[] args) { String s = "-"; switch(TimeZone.CST) { case EST: s += "e"; case CST: s += "c"; case MST: s += "m"; default: s += "X"; case PST: s += "p"; } System.out.println(s); } } enum TimeZone {EST, CST, MST, PST } What is the result?

  1. -c

  2. -X

  3. -cm

  4. -cmp

  5. -cmXp

  6. Compilation fails.

Reveal answer Fill a bubble to check yourself
E Correct answer
Explanation

The switch statement has no break statements, causing fall-through. With TimeZone.CST: case CST matches, appends 'c'. No break, so falls through to case MST, appends 'm'. No break, falls through to default, appends 'X'. No break, falls through to PST, appends 'p'. The final string is '-cmXp'. Fall-through continues through all cases after the match.