Java OCA OCP Practice Question 1762

Question

What, if anything, is wrong with the following code?.

void test(int x) {
  switch (x) {
    case 1:
    case 2:
    case 0:
    default:
    case 4:
  }
}

Select the one correct answer.

  • (a) The variable x does not have the right type for a switch expression.
  • (b) The case label 0 must precede case label 1.
  • (c) Each case section must end with a break statement.
  • (d) The default label must be the last label in the switch statement.
  • (e) The body of the switch statement must contain at least one statement.
  • (f) There is nothing wrong with the code.


(f)

Note

There is nothing wrong with the code.

The case and default labels do not have to be specified in any specific order.

The use of the break statement is not mandatory, and without it the control flow will simply fall through the labels of the switch statement.




PreviousNext

Related