What is the output of following block of program ? boolean var = false; if(var = true) { System.out.println(“TRUE”); } else { System.out.println(“FALSE”); }
Reveal answer
Fill a bubble to check yourself
What is the output of following block of program ? boolean var = false; if(var = true) { System.out.println(“TRUE”); } else { System.out.println(“FALSE”); }
TRUE
true
FALSE
Compilation Error
Run time Error
The code uses assignment (=) not comparison (==). The statement if(var = true) assigns true to var, then evaluates the condition as true, so 'TRUE' is printed. This is a common bug where assignment is mistaken for equality check.
The output is TRUE. In Java, if (var = true) uses a single '=' (assignment), not '==' (comparison) — but this compiles because var is declared as boolean: the assignment expression var = true evaluates to the assigned value, true, which is itself a valid boolean for the if-condition (unlike C, Java doesn't allow non-boolean types in if-conditions, but a boolean assignment expression is fine). So the statement assigns true to var and the if-condition becomes true, executing System.out.println("TRUE") — printing the literal string TRUE exactly. It's not a compile error (that would only happen if var were, say, an int) and it's not FALSE since the branch taken is the true-block.