$ ./guess_the_languageProblem Solving — predict the output
Problem Solving — predict the output · medium · #250

What is the output?

int x = 2;
String r = "";
switch (x) {
  case 1: r += "a";
  case 2: r += "b";
  case 3: r += "c"; break;
  default: r += "d";
}
System.out.println(r);
  1. bc
  2. b
  3. bcd
  4. abc
Execution jumps to case 2 and falls through into case 3 because there is no break, then stops at the break before default.
Play Problem Solving ▸