Java OCA OCP Practice Question 784

Question

What will be the result of attempting to compile and run the following program?

public class Main{ 
   public static void main (String args []){ 
      try{ /*  w  ww. j a va2  s  .c  om*/
         RuntimeException re = null; 
         throw re; 
       } 
      catch (Exception e){ 
         System .out.println (e); 
       } 
    } 
} 

Select 1 option

  • A. fail to compile, since RuntimeException cannot be caught by catching an Exception.
  • B. fail to compile, since re is null.
  • C. compile without error and will print java.lang.RuntimeException when run.
  • D. compile without error and will print java.lang.NullPointerException when run.
  • E. compile without error and will run and print null.


Correct Option is  : D

Note

The try block generates NullPointerException which will be caught by the catch block.




PreviousNext

Related