Java OCA OCP Practice Question 1800

Question

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

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

Select the one correct answer.

  • (a) The code will fail to compile because the main() method does not declare that it throws RuntimeException in its declaration.
  • (b) The program will fail to compile because it cannot throw re.
  • (c) The program will compile without error and will throw java.lang.RuntimeException 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.


(d)

Note

The program will compile without error, but will throw a NullPointerException when run.

The throw statement can only throw Throwable objects.

A NullPointerException will be thrown if the expression of the throw statement results in a null reference.




PreviousNext

Related