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

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

Which of the following correctly uses try-with-resources to ensure a FileInputStream is closed after use?

⚠ Common exam trap

The trap here is that candidates often forget that try-with-resources requires the resource variable to be declared with its type inside the parentheses, and they mistakenly think a pre-declared variable or a variable without a type declaration is valid.

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("file.txt")) { ... }

It uses the try-with-resources syntax (try (resource declaration) { ... }), which automatically closes the FileInputStream when the block exits, regardless of exceptions. This ensures the resource is closed without requiring an explicit finally block, as mandated by the AutoCloseable interface.

Answer analysis

Option-by-option breakdown

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

  • FileInputStream fis = new FileInputStream("file.txt"); try { ... } finally { fis.close(); }

    Why it's wrong here

    Resource declared outside try block; no automatic closure guarantee.

  • try (FileInputStream fis = new FileInputStream("file.txt")) { ... }

    Why this is correct

    Correct syntax; resource is closed automatically.

  • try { FileInputStream fis = new FileInputStream("file.txt"); ... } finally { fis.close(); }

    Why it's wrong here

    Traditional try-finally, not try-with-resources.

  • try (fis = new FileInputStream("file.txt")) { ... }

    Why it's wrong here

    Missing variable type; resource declaration incomplete.

About these practice questions

This 1Z0-829 question is part of Courseiva's 513-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.