Java OCA OCP Practice Question 1513

Question

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

public class Main{ 
   public static void main (String args []){ 
      Exception e = null; 
      throw e; 
    } 
} 

Select 1 option

  • A. The code will fail to compile.
  • B. The program will fail to compile, since it cannot throw a null.
  • C. The program will compile without error and will throw an Exception when run.
  • D. The program will compile without error and will throw java.lang.NullPointerException when run
  • E. The program will compile without error and will run and terminate without any output.


Correct Option is  : A

Note

You are throwing an exception and there is no try or catch block, or a throws clause.

So it will not compile.

If you do either put a try catch block or declare a throws clause for the method then it will throw a NullPointerException at run time because e is null.




PreviousNext

Related