Multiple choice

What is the output of the given Java Program?

public class HelloWorld
{
public static void main(String []args)
{
int i = 1048576>>8>>3>>8>>3;
switch(i++ - 1) // Line 1
{
case -1 : System.out.println("Case -1");
case 1 : System.out.println("Case 1");
default : break;
case 0 : System.out.println("Case 0");
}
}
}

  1. Case -1 Case 1

  2. Case -1Case 1Case 0

  3. Case 0

  4. No output

  5. Error at Line 1

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

This is the correct answer. The expression given in the code (int i = 1048576>>8>>3>>8>>3;) will be evaluated as :-1048576 / (2^8) / (2^3) / (2^8) / (2^3).This will result into a 0.so the value of variable i becomes 0. Now, switch(i++ - 1 ) will result into switch(0 -1), because we have used post-increment variable on variable i, so the value of variable i will be 0 at this time. So the switch case becomes:-Switch(-1).Now, the case label having value -1 will be executed, so “case -1” gets printed on screen. But there is no break statement after Case 1, so Case 2 statements will also get executed and we find a break after default. So no further statements will be executed.So this answer is true. The correct output is Case -1 Case 1.