Multiple choice

What is the output of the given java program?

public class HelloWorld
{
 public static void main(String []args)
{
 int i = 1024<<8<<3>>8>>3; switch(i)
 {
 default : System.out.
println("Default");
case 1048576/(34*32) : System.out.
println("Case 1");
case 1073741824/(2*32*4*32*16*8*2) : System.out.
println("Case 2");
case 1048576/(32*32) : System.out.
println("Case 3");
 }
 }
 }

  1. Default Case 1Case 2Case 3

  2. Case1Case 2Case 3

  3. Case 2Case 3

  4. Case 3

  5. Compiler Error

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

This is the correct choice. The expression (int i = 1024<<8<<3>>8>>3;) given in theCode Evaluates to 1024. So the value of variable i is 1024. The expression will be evaluated as: (1024*(2^8)*(2^3)) / ((2^8)/(2^3)). So, the value of the variable I is now 1024. Now, switch will find a case label having value 1024 and all the Statements of that case will be evaluated. Case 3 is having the value 1024. So, “Case 3” is printed on the screen. So, this answer is correct.