ExamVeda
Login
Home
This question belongs to Java Program Exceptions
Exceptions
?

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();
      }
}

Answer & Solution
Correct Answer: Option C

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

Examveda
Question posted by Examveda
Community

Join the Discussion

No comments yet Be the first to discuss this question.