Vidyalelo
Java Programming · Q48

Exceptions

Programming · Java Programming · question 48

Q48

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
Answer
D.
Compilation succeed but no output
E.
None of these

Answer: Option C

Solution

Answer: Option C
Solution:

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