Q. What is the output of the following code?
Code:
int x = 5;
switch (x) {
case 1:
System.out.println(“x is 1”);
break;
case 5:
System.out.println(“x is 5”);
break;
default:
System.out.println(“x is not 1 or 5”);
}
β
Correct Answer: (B)
x is 5
Explanation: The switch statement tests the value of x against each case, and executes the code for the first matching case. In this case, the code for the case where x equals 5 is executed.