Java OCA OCP Practice Question 1858

Question

What will the following code print when compiled and run?

abstract class Main{
   abstract void m();
   public static void main (String [] args){
      System.out.println ("calculating");
      Main x = null;
      x.m ();
    }
}

Select 1 option

  • A. It will not compile.
  • B. It will not print anything and will throw NullPointerException
  • C. It will print calculating and then throw NullPointerException.
  • D. It will print calculating and will throw NoSuchMethodError
  • E. It will print calculating and will throw MethodNotImplementedException


Correct Option is:C

Note

For A.

It will compile fine.

For C.

After printing, when it tries to call m() on x, it will throw NullPointerException since x is null.




PreviousNext

Related