Java Exceptions: 19 MCQs with answers
All 19 multiple-choice questions from this kit, free and with no sign-up.
Warm-up · 10 questions
Q1. Which keyword is used to manually throw an exception in Java?
BeginnerShow answer & explanation
throw
The `throw` keyword is used to explicitly throw an exception, while `throws` declares exceptions in a method signature.
Q2. What is the superclass of all exceptions in Java?
BeginnerShow answer & explanation
Throwable
`Throwable` is the root class for the exception hierarchy; `Exception` and `Error` both extend it.
Q3. What happens if an exception is not caught in a Java program?
BeginnerShow answer & explanation
The program terminates abruptly
An uncaught exception propagates up the call stack and causes the program to terminate with an error message.
Q4. Which block is always executed regardless of whether an exception occurs or not?
BeginnerShow answer & explanation
finally
The `finally` block is designed to execute cleanup code and always runs after the `try`/`catch` blocks, unless the JVM exits.
Q5. What is the purpose of the `try` block?
BeginnerShow answer & explanation
To enclose code that might throw an exception
The `try` block wraps code that may throw an exception, allowing it to be caught and handled by a `catch` block.
Q6. Which of the following is NOT a type of exception in Java?
BeginnerShow answer & explanation
Compile-time exception
Java categorizes exceptions into checked and unchecked (runtime) exceptions; there is no 'compile-time exception' category.
Q7. Which exception is thrown when an array index is out of bounds?
BeginnerShow answer & explanation
ArrayIndexOutOfBoundsException
`ArrayIndexOutOfBoundsException` is thrown when accessing an array with an illegal index (negative, equal to size, or greater).
Q8. What is the purpose of the `getMessage()` method in an exception object?
BeginnerShow answer & explanation
To return a detailed message about the exception
`getMessage()` returns the detail message string of the exception, which often provides information about the cause.
Q9. A `finally` block is optional in a try-catch construct.
BeginnerShow answer & explanation
True
A `finally` block is not required; you can have a try-catch without it. However, if present, it always executes.
Q10. Checked exceptions are checked at runtime.
BeginnerShow answer & explanation
False
Checked exceptions are checked at compile time; the compiler ensures they are either caught or declared. Runtime exceptions are unchecked.
Building up · 8 questions
Q11. Which of the following is an unchecked exception?
IntermediateShow answer & explanation
ArithmeticException
`ArithmeticException` is a subclass of `RuntimeException`, making it unchecked. The others are checked exceptions.
Q12. Which of the following correctly declares that a method might throw an IOException?
IntermediateShow answer & explanation
public void readFile() throws IOException
The `throws` keyword is used in the method signature to declare that the method may throw a checked exception like IOException.
Q13. Which of the following is a checked exception?
IntermediateShow answer & explanation
FileNotFoundException
`FileNotFoundException` is a subclass of `IOException`, which is a checked exception. The others are unchecked runtime exceptions.
Q14. What does the `finally` block ensure?
IntermediateShow answer & explanation
That cleanup code runs
The `finally` block is used for cleanup activities like closing resources, and it runs regardless of whether an exception is thrown or caught.
Q15. Which class is the parent of `RuntimeException`?
IntermediateShow answer & explanation
Exception
`RuntimeException` extends `Exception`, which in turn extends `Throwable`.
Q16. What is the output of the following code? ```java try { int x = 10 / 0; } catch (ArithmeticException e) { System.out.println("Caught"); } finally { System.out.println("Finally"); } ```
IntermediateShow answer & explanation
Caught Finally
The `ArithmeticException` is caught, printing "Caught", and then the `finally` block runs, printing "Finally".
Q17. Which keyword is used to handle multiple exceptions in a single catch block?
IntermediateShow answer & explanation
|
Java 7+ allows catching multiple exception types in one catch block using the pipe symbol `|`, e.g., `catch (IOException | SQLException e)`.
Q18. What is the difference between `throw` and `throws`?
IntermediateShow answer & explanation
`throw` is used to throw an exception, `throws` declares exceptions in a method signature
`throw` is a statement that actually throws an exception object, while `throws` is a clause in the method signature indicating which exceptions the method may throw.
Topper level · 1 questions
Q19. What happens if a `finally` block contains a `return` statement?
AdvancedShow answer & explanation
The return value from the finally block overrides the try block's return
If a `finally` block has a `return`, it will override any return value from the `try` or `catch` blocks, which can be surprising.
Your notes can be a kit like these
Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.
- Chapter notes
- Flashcards
- Practice MCQs
- Scored tests
- Exam prep
Get Veda free See what students get
Free to start · No ads · Works in 42 languages