QUIZ: Topic 4 - Exception Handling in Java Total Questions: 10 Instructions: Choose the most correct option (A, B, C or D) for each question. Check your answers using the Answer Key at the end. 1. Which class is the root of the Java exception hierarchy, from which Exception and Error are derived? A) RuntimeException B) Throwable C) Exception D) Object 2. Which of the following is a checked exception? A) ArithmeticException B) Error C) SQLException D) NullPointerException 3. Consider a method whose signature is public void getDetails() throws IOException. Which keyword has been used here to inform callers about the exception the method may throw? A) throw B) try C) throws D) finally 4. Which statement correctly distinguishes throw from throws? A) throw is used in the method signature to declare an exception, while throws is used inside the method body to create an exception object. B) throw and throws mean the same thing and can be used interchangeably. C) throw is used inside a method body to actually throw an exception object, while throws is used in the method signature to declare exceptions the method may throw. D) throws is used only in catch blocks, and throw is used only in finally blocks. 5. In try-with-resources, a resource declared in the try(...) parentheses is closed automatically at the end of the statement. What must the resource class implement? A) java.lang.Runnable B) java.lang.Cloneable C) java.lang.AutoCloseable D) java.io.Serializable 6. Which of the following is the correct way to define a custom (user-defined) exception class named InvalidAgeException? A) class InvalidAgeException implements Exception B) class InvalidAgeException extends Exception C) class InvalidAgeException extends RuntimeException only, never Exception D) class InvalidAgeException extends Error 7. A try statement has several catch blocks. Which rule about their order is correct? A) Catch blocks may appear in any order; the order has no effect. B) A catch block for a general type such as Exception must not appear before a catch block for a more specific subtype, otherwise the later block is unreachable and a compile-time error occurs. C) The catch block with the most specific type must always come last, but this is only a suggestion and never an error. D) Only one catch block is allowed for each try block. 8. In a nested try-catch (a try block inside another try block), what happens if an exception occurs in the inner try block and no inner catch block matches it? A) The exception is silently ignored and normal flow continues. B) A compile-time error is reported for the nested structure. C) The program always terminates immediately without any further handling. D) The exception propagates to the outer try block, whose catch handles it if it matches. 9. Which of the following is an example of an Error, which is irrecoverable and not normally handled by application code? A) OutOfMemoryError B) IOException C) SQLException D) ClassNotFoundException 10. Which statement about unchecked exceptions is true? A) The compiler forces the programmer to catch them or declare them in a throws clause. B) They are checked at compile-time, so bugs are caught before the program runs. C) They are not required to be caught or declared, can occur anywhere in the program, and signify flaws in the program's logic such as NullPointerException or ArrayIndexOutOfBoundsException. D) They can only occur inside the main method of a class. ANSWER KEY 1. B) Throwable is the superclass of both Exception and Error, so it is the root of the exception hierarchy. 2. C) SQLException is not a RuntimeException or Error, so the compiler treats it as a checked exception. 3. C) throws is the keyword used in a method signature to declare the checked exceptions the method may throw. 4. C) throw creates and throws an exception instance inside code, while throws declares exception types in the method signature for the caller. 5. C) try-with-resources accepts objects implementing java.lang.AutoCloseable, which are closed automatically at the end of the statement. 6. B) A custom exception is created by extending Exception or one of its subclasses, which makes it a checked exception. 7. B) A superclass catch block placed before a subclass catch block makes the subclass block unreachable, which is a compile-time error. 8. D) An exception not caught by an inner handler propagates outward to the enclosing try block for handling. 9. A) Errors such as OutOfMemoryError and VirtualMachineError are irrecoverable conditions, unlike the listed checked exceptions. 10. C) Unchecked exceptions (RuntimeException, Error and subclasses) are not checked by the compiler, so no try-catch or throws is mandatory.