ProgrammingModerateAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
String s = "b";
int r = 0;
switch (s) {
    case "a": r += 1;
    case "b": r += 2;
    case "c": r += 4; break;
    default:  r += 8;
}
System.out.print(r);
  1. A. 2
  2. B. 6
  3. C. 14
  4. D. 7

Correct answer: B. 6

Execution enters at case "b" and falls through to case "c" because there is no break, adding 2 + 4 = 6; the break then stops before default.

More OOP with C++ and Java questions

Exams that ask this topic