Java OCA OCP Practice Question 677

Question

Consider the following code snippet:

XXXX m ; 
  switch ( m ){ 
     case 32   : System.out.println ("32");   break; 
     case 64   : System.out.println ("64");   break; 
     case 128  : System.out.println ("128");  break; 
   } 

What type can 'm' be of so that the above code compiles and runs as expected ?

Select 3 options

  • A. int m;
  • B. long m;
  • C. char m;
  • D. byte m;
  • E. short m;


Correct Options are  : A C E

Note

Only byte, char, short, int, and enum values can be used as types of a switch variable.

Java 7 allows String as well.

The switch variable must be big enough to hold all the case constants.

if switch variable is of type char, then none of the case constants can be greater than 65535 because char's range is from 0 to 65535.

All case labels should be Compile Time Constants.




PreviousNext

Related