Courseiva
Java I/O API and Securing ApplicationseasyMultiple ChoiceObjective-mapped

1Z0-829 Java I/O API and Securing Applications Practice Question

Which resource declaration order in try-with-resources is valid when both a FileInputStream and a BufferedInputStream wrapping it need to be closed automatically?

⚠ Common exam trap

Oracle often tests the misconception that resources declared inside the try block (not in the resource list) are automatically closed, or that the order of declaration in try-with-resources does not matter for wrapping streams.

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

try (FileInputStream fis = new FileInputStream("a"); BufferedInputStream bis = new BufferedInputStream(fis)) { ... }

In a try-with-resources statement, resources are declared in order and closed in reverse order. By declaring FileInputStream first and then BufferedInputStream wrapping it, both resources are properly closed automatically, with the BufferedInputStream closed first (ensuring its buffer is flushed) and then the FileInputStream.

Answer analysis

Option-by-option breakdown

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

  • try (FileInputStream fis = new FileInputStream("a")) { BufferedInputStream bis = new BufferedInputStream(fis); ... }

    Why it's wrong here

    bis is not in the try header, so it is not automatically closed.

  • try (FileInputStream fis = new FileInputStream("a"); BufferedInputStream bis = new BufferedInputStream(fis)) { ... }

    Why this is correct

    Both resources are declared in order; fis is closed after bis as resources are closed in reverse order.

  • try (BufferedInputStream bis = new BufferedInputStream(fis); FileInputStream fis = new FileInputStream("a")) { ... }

    Why it's wrong here

    fis is used before declaration, causing a compilation error.

  • try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a"))) { ... }

    Why it's wrong here

    Only one resource is declared, not both as required.

About these practice questions

Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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-829 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-829 exam.