Java OCA OCP Practice Question 1905

Question

What is the result of executing the following fragment of code:.

boolean b1 = false;
int i1 = 2;
int i2 = 3;
if  (b1 = i1 == i2){
   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.


Correct Option is  : C

Note

All an if statement needs is a boolean.

Now i 1 == i2 returns false which is a boolean and since b 1 = false is an expression and every expression has a return value, it returns false which is again a boolean.

Therefore, in this case, the else condition will be executed.




PreviousNext

Related