A

Admin • 828.03K Points
Coach

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”);
}
  • (A) x is 1
  • (B) x is 5
  • (C) x is not 1 or 5
  • (D) There is a syntax error in the code.
  • Correct Answer - Option(B)
  • Views: 15
  • Filed under category Java
  • Hashtags:

Explanation by: Admin
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.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.