Courseiva
Question 191 of 481
Exception Handling and Development ToolsmediumMultiple ChoiceObjective-mapped

Try-With-Resources: Automatic Resource Management in Java

A developer writes a method that reads a file and must ensure the file is closed even if an exception occurs. Which construct should be used?

Quick Answer

The answer is the try-with-resources statement because it guarantees automatic resource closure even when an exception occurs. This construct works by declaring resources that implement the AutoCloseable interface within the try parentheses, and the Java runtime automatically calls their close() method when the block exits, whether normally or abruptly due to an exception. On the Oracle Java Foundations 1Z0-811 exam, this question tests your understanding of robust resource management, often appearing as a scenario where a file or database connection must be safely released. A common trap is choosing a try-catch-finally block, which requires an explicit close() call that can be skipped if an exception fires before that line. Remember the memory tip: "AutoCloseable in the try, no finally needed to say goodbye."

⚠ Common exam trap

Oracle often tests whether candidates recognize that a `finally` block with explicit `close()` is not the best practice compared to try-with-resources, even though it technically works, because the exam emphasizes modern, concise, and safe resource management.

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

A try-with-resources statement

The try-with-resources statement automatically closes any resource that implements `AutoCloseable` (such as `FileReader` or `BufferedReader`) at the end of the statement, even if an exception occurs. This eliminates the need for explicit cleanup code and ensures deterministic resource management without relying on a `finally` block.

Answer analysis

Option-by-option breakdown

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

  • A throws declaration in the method signature

    Why it's wrong here

    throws only propagates the exception, does not close resources.

  • A finally block without any resource declaration

    Why it's wrong here

    Finally alone does not automatically close resources.

  • A try-with-resources statement

    Why this is correct

    try-with-resources automatically closes resources implementing AutoCloseable.

  • A try-catch-finally block with explicit close in finally

    Why it's wrong here

    Explicit close might be missed if not properly written.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more way this is tested on 1Z0-811

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. When using try-with-resources, which interface must the resource implement?

medium
  • A.Serializable
  • B.Runnable
  • C.AutoCloseable
  • D.Comparable

Why C: The try-with-resources statement in Java requires that any resource declared in its parentheses implements the `AutoCloseable` interface (or its subinterface `Closeable`). This interface defines a single `close()` method, which the JVM automatically invokes at the end of the try block, ensuring proper resource management without needing an explicit `finally` block.

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.