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(1 + i++ - 1) // Line 1
{
case 0 : System.out.println(1024<<8<<3>>8>>3);
case -1 : System.out.println(1024<<2>>3<<2);
default : break;
case 1 : System.out.println(1024<<3<<8>>3>>3);
}
-
32768
-
10242048
-
2048
-
No Output
-
Error at Line 1
B
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(1 + i++ - 1 ) will result into switch(1 + 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(0).Now the Case having label 0 will get executed and it will solve the given expression (1024<<8<<3>>8>>3) as (1024*(2^8)(2^3)) / ((2^8)/(2^3)). So it will print 1024.As we don’t have any break after case 1. So case 2 statements will also get executed and the expression in case 2 (1024<<2>>3<<2) will be evaluated as (1024(2^2) / (2^3) *(2^2)) to result 2048.Now we encountered break after default case.So the program terminated here. So the correct output is :-10242048. So this answer is true.