Java OCA OCP Practice Question 2978

Question

Given:

public class Main {
   static boolean b1 = false;
   static int z = 7;
   static Long y;

   public static void main(String[] args) {
      for (int i = 0; i < 4; i++)
         go(i);/*from  ww  w.j av  a 2  s.c om*/
   }

   static void go(int x) {
      try {
         if ((x == 0) && (!b1 && z == 7))
            System.out.print("0 ");
         if (x < 2 ^ x < 10)
            System.out.print("1 ");
         if ((x == 2) && (y == null | (y.longValue() == 0)))
            System.out.print("2 ");
         if (z <= (x + 4))
            System.out.print("3 ");
      } catch (Exception e) {
         System.out.print("e ");
      }
   }
}

What is the result?

  • A. 0 1 2 3
  • B. 1 e 1 3
  • C. 0 1 e 1 3
  • D. 0 1 1 1 1 3
  • E. 1 1 1 2 1 3
  • F. 0 1 1 1 2 1 3
  • G. Compilation fails.


C is correct.

Note

The ^ is true only when exactly one of the tests is true.

The | does NOT short circuit, so longValue() is invoked and throws a NullPointerException.




PreviousNext

Related