A

Admin • 828.70K Points
Coach

Q. What is the output for the below code?
import java.io.FileNotFoundException;
class A{
public void printName() throws FileNotFoundException{
System.out.println("Value-A");
}
}
class B extends A{
public void printName() throws NullPointerException{
System.out.println("Name-B");
}
}
public class Test{
public static void main (String[] args) throws Exception{
A a = new B();
a.printName();
}
}

  • (A) Value-A
  • (B) Compilation fails-Exception NullPointerException is not compatible with throws clause in A.printName()
  • (C) Name-B
  • (D) Name-B D. Compilation succeed but no output
  • Correct Answer - Option(C)
  • Views: 12
  • Filed under category Java
  • Hashtags:

Explanation by: Admin
The overriding method can throw any unchecked (runtime) exception, regardless of exception thrown by overridden method. NullPointerException is RuntimeException so compiler will not complain.

You must be Logged in to update hint/solution

Discusssion

Login to discuss.

Be the first to start discuss.