Java OCA OCP Practice Question 495

Question

What variable type of red allows the following application to compile?


public class MyClass { 
         public static void main(String[] args) { 
            int myField = 10; 
                     red = 5; /*from   w  w  w. j  a v a 2s  .  c om*/
           switch(myField) { 
              default: 
                 System.out.print("Home"); 
                 break; 
              case red: 
                 System.out.print("Away"); 
           } 
        } 
} 
  • A. long
  • B. double
  • C. int
  • D. None of the above


D.

Note

The value of a case statement must be constant, a literal value, or final variable.

Since red is missing the final attribute, no variable type allows the code to compile, making Option D the correct answer.




PreviousNext

Related