Java OCA OCP Practice Question 2437

Question

What's the output of the following code?

public class Main { 
    public static void main(String args[]) { 
        int num = 120; 
        switch (num) { 
            default: System.out.println("default"); 
            case 0: System.out.println("case1"); 
            case 10*2-20: System.out.println("case2"); 
            break; 
        } //from w w w .  j  a v a 2  s  .c om
    } 
} 
a  default //from   w w w . ja va  2 s .  com
   case1 
   case2 

b  case1 
   case2 

c  case2 

d  Compilation error 
e  Runtime exception 


d

Note

The expressions used for both case labels-0 and 10*2-20-evaluate to the constant value 0.

Because you can't define duplicate case labels for the switch statement, the code will fail to compile with an error message that states that the code defines a duplicate case label.




PreviousNext

Related