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();
}
}
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. Compilation succeed but no output
E. None of these
Answer: Option C
Solution(By Examveda Team)
The overriding method can throw any unchecked (runtime) exception, regardless of exception thrown by overridden method. NullPointerException is RuntimeException so compiler will not complain.
A. An error that occurs during runtime
B. An error that occurs during compilation
C. A warning issued by the compiler
D. A type of loop
What is the purpose of the "try" block in exception handling?
A. It catches exceptions and handles them
B. It specifies the exception type
C. It contains the code that might throw an exception
D. It specifies the exception message
In Java, can a method declare multiple exceptions using the "throws" keyword?
A. Only if the method is marked as "static"
B. Yes, a method can declare multiple exceptions separated by commas
C. Only if the exceptions are marked as "final"
D. Only if the method is marked as "static"
Join The Discussion