Java OCA OCP Practice Question 628

Question

The following class will print 'index = 2' when compiled and run.

class Test{ /*from  w ww  .j a  va2s.  co  m*/
   public static int [] getArray ()  {  return null;   } 
   public static void main (String [] args){ 
      int index = 1; 
      try{ 
         getArray ()[index=2]++; 
       } 
      catch  (Exception e){   }  //empty catch 
      System .out.println ("index = " + index); 
    } 
} 

Select 1 option

  • A. True
  • B. False


Correct Option is  : A

Note

If the array reference expression produces null instead of a reference to an array,

then a NullPointerException is thrown at runtime,

but only after all parts of the array reference expression have been evaluated and only if these evaluations completed normally.

first index = 2 will be executed, which assigns 2 to index.

After that null[2] is executed, which throws a NullPointerException.

But this exception is caught by the catch block, which prints nothing.

So it seems like NullPointerException is not thrown but it actually is.

The embedded assignment of 2 to index occurs before the check for array reference produced by getArray().

In an array access, the expression to the left of the brackets appears to be fully evaluated before any part of the expression within the brackets is evaluated.

If evaluation of the expression to the left of the brackets completes abruptly, no part of the expression within the brackets will appear to have been evaluated.




PreviousNext

Related