Java OCA OCP Practice Question 1018

Question

The following method will compile and run without any problems.

public void switchTest (byte x){ 
   switch (x){ 
      case  'b ':   // 1 
      default  :   // 2 
      case -2:    // 3 
      case 80:    // 4 
    } 
} 

Select 1 option

  • A. True
  • B. False


Correct Option is  : A

Note

The following types can be used as a switch variable:

byte, char, short, int, String, and enums.

Note that long, float, double, and boolean are not allowed.

All the case constants should be assignable to the switch variable type.

had there been a case label of 128 ( case 128 : //some code ), it would not have compiled.

Because the range of a byte is from - 128 to 127 and so 128 is not assignable to 'x'.

The internal value of 'b' is 98, which is less than 127 so Line // 1 is fine.




PreviousNext

Related