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");
}
}
}
-
Case -1Case 1Case 0
-
Case -1Case 1
-
Case 0
-
Case 1
-
Compiler error.
E
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).But we are using string values in case labels to find out the matching case. So the compiler will raise an error that “Required int, Found String”. So this answer is correct.