Courseiva
Exception Handling and Development ToolsmediumMultiple ChoiceObjective-mapped

1Z0-811 Exception Handling and Development Tools Practice Question

A developer writes a method that reads a file and processes its contents. The method uses a BufferedReader wrapped around a FileReader. Which of the following approaches ensures that both resources are properly closed even if an exception occurs?

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Use try-with-resources with both resources declared in the try header.

Try-with-resources ensures that both the BufferedReader and FileReader are automatically closed in reverse order, even if an exception occurs. Option A is wrong because while it attempts to close both resources in separate try-catch blocks within a finally block, this approach is verbose and error-prone; if an exception occurs during the first close, the second close may be skipped unless handled carefully, and it does not guarantee resource closure as reliably as try-with-resources. Option C is wrong because closing only the BufferedReader may close the underlying FileReader due to chaining, but this approach lacks null-safety—if resource creation fails, a NullPointerException occurs when calling close, and it does not demonstrate proper exception handling or best practices. Option D is wrong because throwing a custom exception after closing resources does not ensure both are closed; it adds unnecessary complexity and may leave resources open if close methods throw exceptions.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Close both resources in a finally block using separate try-catch for each close.

    Why it's wrong here

    This is verbose and error-prone; if resource creation fails, variables may be null leading to NullPointerException.

  • Use try-with-resources with both resources declared in the try header.

    Why this is correct

    try-with-resources ensures that each resource is closed automatically, in reverse order, even if an exception occurs.

  • Close only the BufferedReader in a finally block.

    Why it's wrong here

    Closing only the outer resource may not close the underlying FileReader, causing a resource leak.

  • Throw a custom exception after closing resources in the catch block.

    Why it's wrong here

    If an exception occurs before close statements, resources remain open; also, throwing after close bypasses cleanup if an earlier exception occurred.

About these practice questions

One of 481 original 1Z0-811 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This 1Z0-811 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-811 exam.