Java OCA OCP Practice Question 771

Question

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

public class Main {
   void test(int x) {
      switch (x) {
      case 1://from w  ww .jav  a  2s  .  c  om
      case 2:
      case 0:
      default:
      case 4:
      }
   }
}

Select 1 option

  • A. Data Type of 'x' is not valid to be used as an expression for the switch clause.
  • B. The case label 0 must precede case label 1.
  • C. Each case section must end with a break keyword.
  • D. The default label must be the last label in the switch statement.
  • E. There is nothing wrong with the code.


Correct Option is  : E

Note

For Option A.

x is an int and int is perfectly valid. long, double, boolean, and float are not valid.

For Option B.

While ordering may be important for the logic being implemented in the code, technically, any order is valid.

For Option C.

This is not necessary.

If there is no break at the end of a case section, the control will fall through to the next case section (even if the case label doesn't match).

For Option D.

Any order of case statements is valid.

E. There is nothing wrong with the code.




PreviousNext

Related