What's the output of the following code?
int a = 10; if (a++ > 10) { System.out.println("true"); } { System.out.println("false"); } System.out.println("ABC");
a true //w w w.j a v a2 s . c o m false ABC b false ABC c true ABC d Compilation error
b
First of all, the code has no compilation errors.
This question has a trick-the following code snippet isn't part of the if construct:.
{
System.out.println("false");
}
Hence, the value false will print no matter what, regardless of whether the condition in the if construct evaluates to true or false.
Because the opening and closing braces for this code snippet are placed right after the if construct, it leads you to believe that this code snippet is the else part of the if construct.
Also, note that an if construct uses the keyword else to define the else part.
This keyword is missing in this question.
The if condition (that is, a++ > 10) evaluates to false because the postfix increment operator (a++) increments the value of the variable a immediately after its earlier value is used.
10 isn't greater than 10, so this condition evaluates to false.