Java OCA OCP Practice Question 2435

Question

What's the output of the following code?

public class Main { 
    public static void main(String args[]) { 
        int num = 20; 
        final int num2; 
        num2 = 20; //w w  w  .  j a va 2  s  .  co  m
        switch (num) { 
            default: System.out.println("default"); 
            case num2: System.out.println(4); 
            break; 
        } 
    } 
} 
a  default 

b  default 
   4 

c  4 

d  Compilation error 


d

Note

The code will fail to compile.

The case labels require compile-time constant values, and the variable num2 doesn't qualify as such.

Although the variable num2 is defined as a final variable, it isn't assigned a value with its declaration.

The code assigns a literal value 20 to this variable after its declaration, but it isn't considered to be a compile-time constant by the Java compiler.




PreviousNext

Related