Java OCA OCP Practice Question 780


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

public class Main {
   public static void main(String[] args) {
      boolean b1 = false;
      boolean b2 = false;
      if (b2 = b1 == false) {
      } else {/*from www.  j a v  a  2 s.c  o m*/

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  : B


if statement needs is a boolean value.

b1 == false returns true, which is a boolean.

Since b2 = true is an expression and every expression has a return value, which is the Left Hand Side of the expression.

It returns true, which is a boolean.

The return value of expression i = 10; is 10 (an int).