Java Exception Handling Quiz - MCQ - Multiple Choice Questions


Exception handling is an important aspect of Java programming that allows you to handle and manage unexpected runtime errors. In this blog post, we present a Java Exception Handling Quiz comprising 10+ multiple-choice questions (MCQ). This quiz aims to assess your understanding of exception handling in Java, including the basics of exceptions, try-catch blocks, and exception propagation. Let's put your knowledge of Java exception handling to the test!.

Learn and Master Java Programming: Learn Java Programming with Examples.

Learn everything about Java 8 features: Java 8 Tutorial and Examples.

Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills.

YouTube Video - Java Exception Handling Quiz

Subscribe to my YouTube channel to learn more: https://www.youtube.com/@javaguides.

1. What is an exception in Java?

a) An error that occurs during compilation
b) An unexpected event that occurs during program execution
c) A logical error in the code
d) A warning issued by the compiler

Answer:

b) An unexpected event that occurs during program execution

Explanation:

An exception in Java is an unexpected event that occurs during the execution of a program, disrupting its normal flow.

2. Which class is the superclass of all exceptions in Java?

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

Answer:

c) Throwable

Explanation:

The Throwable class is the superclass of all exceptions and errors in Java. It is the root class from which all exception classes are derived.

3. Which type of exception is checked at compile-time?

a) RuntimeException
b) Checked Exception
c) Unchecked Exception
d) Error

Answer:

b) Checked Exception

Explanation:

Checked exceptions are exceptions that are checked at compile-time. The compiler ensures that these exceptions are either caught or declared in the method signature.

4. Which keyword is used to handle exceptions in Java?

a) handle
b) try
c) catch
d) throws

Answer:

b) try

Explanation:

The try keyword is used to handle exceptions in Java. It allows you to define a block of code that will be tested for exceptions.

5. Which block must follow a try block?

a) final
b) catch
c) done
d) except

Answer:

b) catch

Explanation:

A catch block must follow a try block. It is used to handle the exception that occurs in the try block.

6. What does the finally block do in exception handling?

a) Catches the exception
b) Executes only if an exception is thrown
c) Executes code after the try-catch block, regardless of an exception
d) Returns the exception to the caller

Answer:

c) Executes code after the try-catch block, regardless of an exception

Explanation:

The finally block is used to execute important code such as closing resources. It is executed after the try-catch block, whether an exception is thrown or not.

7. Which keyword is used to throw an exception explicitly in Java?

a) throw
b) throws
c) try
d) catch

Answer:

a) throw

Explanation:

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

8. Which keyword is used to declare an exception in the method signature?

a) throw
b) throws
c) try
d) catch

Answer:

b) throws

Explanation:

The throws keyword is used in the method signature to declare that a method can throw exceptions.

9. What type of exception is thrown when a null reference is accessed?

a) NullPointerException
b) IllegalArgumentException
c) ArrayIndexOutOfBoundsException
d) ClassCastException

Answer:

a) NullPointerException

Explanation:

A NullPointerException is thrown when an application attempts to use an object reference that has the null value.

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

a) IOException
b) SQLException
c) NullPointerException
d) FileNotFoundException

Answer:

c) NullPointerException

Explanation:

NullPointerException is an unchecked exception in Java. It occurs during runtime and does not need to be declared in a method's throws clause.

11. Which exception is thrown when an array is accessed with an illegal index?

a) ArrayIndexOutOfBoundsException
b) ClassCastException
c) NullPointerException
d) ArithmeticException

Answer:

a) ArrayIndexOutOfBoundsException

Explanation:

An ArrayIndexOutOfBoundsException is thrown when an array is accessed with an illegal index that is either negative or greater than the size of the array.

12. Which exception is thrown when an arithmetic operation is attempted to divide by zero?

a) ArithmeticException
b) NumberFormatException
c) ClassCastException
d) NullPointerException

Answer:

a) ArithmeticException

Explanation:

An ArithmeticException is thrown when an arithmetic operation is attempted, such as division by zero.

13. Which exception is thrown when an attempt is made to cast an object to a subclass of which it is not an instance?

a) ClassCastException
b) IllegalArgumentException
c) NumberFormatException
d) ArrayStoreException

Answer:

a) ClassCastException

Explanation:

A ClassCastException is thrown when an attempt is made to cast an object to a subclass of which it is not an instance.

14. Which exception is thrown by the JVM when it encounters an abnormal condition?

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

Answer:

c) Error

Explanation:

An Error is thrown by the JVM when it encounters an abnormal condition that it cannot recover from, such as OutOfMemoryError.

15. Which of the following is a checked exception in Java?

a) IOException
b) ArrayIndexOutOfBoundsException
c) ClassCastException
d) NullPointerException

Answer:

a) IOException

Explanation:

IOException is a checked exception in Java. It must be caught or declared to be thrown in the method signature.

16. Which keyword is used to create a custom exception in Java?

a) extends
b) implements
c) throws
d) super

Answer:

a) extends

Explanation:

To create a custom exception in Java, you need to create a class that extends the Exception class or its subclasses.

17. What happens if an exception is not handled in a program?

a) The program runs normally
b) The program prints an error and continues
c) The program terminates abnormally
d) The program ignores the exception

Answer:

c) The program terminates abnormally

Explanation:

If an exception is not handled in a program, it propagates up the call stack, and if not handled, it causes the program to terminate abnormally.

18. Which exception is thrown when a string cannot be converted to a numeric type?

a) NumberFormatException
b) ClassCastException
c) IllegalArgumentException
d) ArrayIndexOutOfBoundsException

Answer:

a) NumberFormatException

Explanation:

A NumberFormatException is thrown when an attempt is made to convert a string to a numeric type but the string does not have the appropriate format.

19. Which of the following exceptions is thrown when a file is not found?

a) FileNotFoundException
b) IOException
c) NullPointerException
d) ClassCastException

Answer:

a) FileNotFoundException

Explanation:

A FileNotFoundException is thrown when an attempt to open the file denoted by a specified pathname has failed.

20. Which of the following is used to group multiple exceptions in a single catch block?

a) Using the | operator
b) Using the & operator
c) Using the && operator
d) Using the || operator

Answer:

a) Using the | operator

Explanation:

Java allows multiple exceptions to be caught in a single catch block by separating them with the | operator.

21. Which of the following exceptions is thrown when an application attempts to use a method that is not supported by that object?

a) UnsupportedOperationException
b) ClassCastException
c) NullPointerException
d) ArrayIndexOutOfBoundsException

Answer:

a) UnsupportedOperationException

Explanation:

An UnsupportedOperationException is thrown when an application attempts to use a method that is not supported by that object.

22. Which of the following is true about the finally block?

a) It is executed only if an exception is thrown
b) It is executed only if an exception is not thrown
c) It is executed whether an exception is thrown or not
d) It is never executed

Answer:

c) It is executed whether an exception is thrown or not

Explanation:

The finally block is executed whether an exception is thrown or not, ensuring that essential code is always executed, such as closing resources.

23. Which exception is thrown by the JVM when the JVM runs out of memory?

a) OutOfMemoryError
b) StackOverflowError
c) ArithmeticException
d) IOException

Answer:

a) OutOfMemoryError

Explanation:

An OutOfMemoryError is thrown when the JVM cannot allocate an object because it is out of memory and the garbage collector cannot make any space available.

24. Which exception is thrown when there is an attempt to access a class that does not exist?

a) ClassNotFoundException
b) NoClassDefFoundError
c) ClassCastException
d) FileNotFoundException

Answer:

a) ClassNotFoundException

Explanation:

A ClassNotFoundException is thrown when an application tries to load a class through its string name but cannot find the definition for the class.

25. Which exception is thrown when an input-output operation fails or is interrupted?

a) IOException
b) SQLException
c) NullPointerException
d) FileNotFoundException

Answer:

a) IOException

Explanation:

An IOException is thrown when an input-output operation fails or is interrupted, such as when trying to read from a file that does not exist.



Comments