Java OCA OCP Practice Question 2726

Question

What will be printed out when the following code is executed?

switch (5) {//www .ja  v a  2s. c  o m
    case 0:
       System.out.println("zero");
       break;
    case 1:
       System.out.println("one");
    default:
       System.out.println("default");
    case 2:
       System.out.println("two");
}
  • a. one
  • b. default and two
  • c. one, two, and default
  • d. Nothing, a compile-time error is generated


b

Note

The default case can be positioned anywhere within the switch.

As all of the cases, except the first one, are missing a break statement, flow falls through each of the last three cases.

While it is not common, constants can be used for switch statements.




PreviousNext

Related