OCA Java SE 8 Mock Exam 2 - OCA Mock Question 36








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;
    switch (num) {
    default:
      System.out.println("default");
    case num2:
      System.out.println(4);
      break;
    }
  }
}

    a  default 

    b  default 
       4 

    c  4 

    d  Compilation error 




Answer



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.

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 after its declaration, but it isn't considered to be a compile-time constant by the Java compiler.