ProgrammingHardWipro · Cognizant technical pattern

What is printed?

What is printed?
String s = "b";
switch (s) {
    case "a": System.out.print("A");
    case "b": System.out.print("B");
    case "c": System.out.print("C"); break;
    default:  System.out.print("D");
}
  1. A. B
  2. B. BCD
  3. C. D
  4. D. BC

Correct answer: D. BC

The switch jumps to case "b" and, with no break, falls through into case "c", printing C, then stops at its break. default is never reached.

More OOP with C++ and Java questions

Exams that ask this topic