Java MCQs – Exception Handling

11.) Which of the following is an unchecked exception in Java?

A) IOException
B) NullPointerException
C) FileNotFoundException
D) ClassNotFoundException

Answer: Option B

Explanation: “NullPointerException” is an unchecked exception in Java.

12.) What happens if an exception is not caught or declared in a method that throws it?

A) The program will terminate.
B) The compiler will generate an error.
C) The exception is automatically caught and handled by the JVM.
D) It is a runtime exception and does not need to be caught or declared.

Answer: Option B

Explanation: The compiler will generate an error if an exception is not caught or declared in a method that throws it.

13.) Which exception is thrown when a method is called on an object that is null?

A) NullReferenceException
B) NullPointerException
C) ObjectNotFoundException
D) InvalidObjectException

Answer: Option B

Explanation: “NullPointerException” is thrown when a method is called on an object that is null.

14.) What is the purpose of the “getMessage()” method in the Exception class?

A) It throws an exception.
B) It catches exceptions.
C) It returns a message associated with the exception.
D) It terminates the program.

Answer: Option C

Explanation: The “getMessage()” method in the Exception class returns a message associated with the exception.

15.) What happens if an exception is thrown inside a “finally” block?

A) The exception is caught and handled within the “finally” block.
B) The exception is propagated to the caller method.
C) The program will terminate.
D) The exception is suppressed, and the program continues execution.

Answer: Option B

Explanation: If an exception is thrown inside a “finally” block, it will propagate to the caller method, potentially overriding any previous exceptions.

16.) What does the “try-with-resources” statement in Java do?

A) It is used to catch exceptions.
B) It is used to create custom exceptions.
C) It is used to manage resources that need to be closed after they are no longer needed.
D) It is used to handle unchecked exceptions.

Answer: Option C

Explanation: The “try-with-resources” statement is used to manage resources that need to be closed after they are no longer needed.

17.) Which exception is thrown when a method is called with an argument of an inappropriate type?

A) TypeMismatchException
B) IllegalArgumentException
C) ClassCastException
D) ArgumentTypeException

Answer: Option B

Explanation: “IllegalArgumentException” is thrown when a method is called with an argument of an inappropriate type.

18.) Which class is used to handle input and output exceptions in Java?

A) IOException
B) FileException
C) InputOutputException
D) ExceptionIO

Answer: Option A

Explanation: “IOException” is used to handle input and output exceptions in Java.

19.) What is the purpose of the “printStackTrace()” method in Java exceptions?

A) It prints the exception message.
B) It prints the stack trace of the exception.
C) It prints the line of code that caused the exception.
D) It prints the name of the exception class.

Answer: Option B

Explanation: The “printStackTrace()” method prints the stack trace of the exception, which includes the method call hierarchy.

20.) When should you use the “throws” clause in a method signature?

A) When you want to create a custom exception
B) When you want to catch exceptions
C) When you want to indicate that the method may throw exceptions
D) When you want to terminate the program

Answer: Option C

Explanation: The “throws” clause is used to indicate that the method may throw exceptions.

Leave a Reply

Your email address will not be published. Required fields are marked *