Submitted by c-admin on Tue, 05/28/2019 - 06:18
Question:
What is the result of executing the following fragment of code:
boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
System.out.println("true");
}else{
System.out.println("false");
} Select 1 option
A. Compile time error.
B. It will print true.
C. It will print false.
D. Runtime error.
E. It will print nothing.
Answer and Explanation (Click to expand) Explanation:
Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.) so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegal because false is a value and not a variable!
Had it been something like (b2 = b1 != b2) then it is valid because it will boil down to : b2 = false.
Because all an if() needs is a boolean, now b1 != b2 returns false which is a Boolean and as b2 = false is an expression and every expression has a return value (which is actually the Left Hand Side of the expression). Here, it returns false, which is again a boolean.
Note that return value of expression : i = 10 , where i is an int, is 10 (an int).