Java Exception Handling MCQ

Java is a language that emphasizes robustness, and a significant part of this comes from its exception handling mechanism. Exception handling allows a program to gracefully handle runtime errors, ensuring the application can continue running or terminate gracefully. Are you ready to test your knowledge on Java’s exception handling? Dive into this beginner-friendly quiz!

Each question is followed by the correct answer and an explanation to help reinforce your knowledge.

1. Which keyword is used to manually throw an exception in Java?

a) throw
b) throws
c) toss
d) trigger

Answer:

a) throw

Explanation:

The throw keyword is used to manually throw an exception in Java.

2. Which of the following is a superclass of all exception classes?

a) RuntimeException
b) Error
c) Throwable
d) Exception

Answer:

c) Throwable

Explanation:

The Throwable class is the superclass of all exception and error classes in Java.

3. What does the finally block do?

a) Catches exceptions
b) Throws exceptions
c) Executes code regardless of an exception occurrence
d) Executes code only when an exception occurs

Answer:

c) Executes code regardless of an exception occurrence

Explanation:

The finally block is used to execute code regardless of whether an exception has occurred or not.

4. Which of these is a checked exception in Java?

a) ArithmeticException
b) NullPointerException
c) IOException
d) IndexOutOfBoundsException

Answer:

c) IOException

Explanation:

IOException is a checked exception. Checked exceptions need to be either caught or declared using the throws keyword.

5. How many catch blocks can a try block have?

a) 0
b) 1
c) 3
d) As many as needed

Answer:

d) As many as needed

Explanation:

A try block can be followed by multiple catch blocks to handle different types of exceptions.

6. What is the purpose of the throws keyword?

a) To throw an exception manually
b) To propagate an exception
c) To catch an exception
d) To define a new exception

Answer:

b) To propagate an exception

Explanation:

The throws keyword is used in the method signature to declare that the method might throw the specified exception, thus propagating it to the caller.

7. Which of these is an unchecked exception?

a) ClassNotFoundException
b) InstantiationException
c) ArrayIndexOutOfBoundsException
d) InterruptedException

Answer:

c) ArrayIndexOutOfBoundsException

Explanation:

ArrayIndexOutOfBoundsException is an unchecked exception. Unchecked exceptions are subclasses of RuntimeException.

8. Which block must be defined first, a catch block or a finally block?

a) catch
b) finally
c) Either can be defined first
d) Both must be defined together

Answer:

a) catch

Explanation:

If both catch and finally blocks are present, the catch block must come before the finally block.

9. If a method does not handle a checked exception, what must the method do?

a) Call another method
b) Use the throw keyword
c) Use the throws keyword in its signature
d) Use a default exception handler

Answer:

c) Use the throws keyword in its signature

Explanation:

If a method does not handle a checked exception, it must declare it using the throws keyword.

10. Which of these is not an exception-handling keyword in Java?

a) try
b) catch
c) exit
d) finally

Answer:

c) exit

Explanation:

exit is not an exception-handling keyword in Java. It is a method (System.exit()) used to exit the program.

I hope this quiz was a valuable tool to gauge and improve your understanding of Java’s exception-handling mechanism. Keep practicing and dive deeper into the world of Java programming!


Leave a Comment

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

Scroll to Top