Java OCA OCP Practice Question 1171

Question

What line of output is displayed by the following program?

public class Main {
   static boolean display(boolean b) {
      System.out.print(" side effect ");
      return b;/*from   ww  w  .jav a  2  s.  co  m*/
   }

   public static void main(String[] args) {
      boolean b1 = true;
      boolean b2 = false;
      if (b2 & display(b1))
         System.out.println(1);
      else if (b1 | display(b2))
         System.out.println(2);
   }

}
  • A. 1
  • B. 2
  • C. side effect 1
  • D. side effect 2
  • E. side effect side effect 1
  • F. side effect side effect 2


F.

Note

Because we are not using the logical short-circuit operators, the display() method is invoked in both cases, resulting in " side effect side effect 2" being displayed.




PreviousNext

Related