Java OCA OCP Practice Question 1398

Question

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  != b2){ 
         System .out.println ("true"); 
      } else{ //from w ww  .ja v a  2s  . com
         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 () needs is a boolean.

Now, b1 != b2 returns false which is a boolean and so the expression becomes b2 = false.

It returns false which is again a boolean.

So there is no error and it prints false.

Every expression has a return value. It is the Left Hand Side of the expression.

For example, The value of the expressing i = 10 , is 10 (an int).




PreviousNext

Related