Courseiva
Exception Handling and Development ToolshardMultiple ChoiceObjective-mapped

1Z0-811 Exception Handling and Development Tools Practice Question

Exhibit

Refer to the exhibit.

```
$ javac MyApp.java
MyApp.java:5: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
        FileReader fr = new FileReader("file.txt");
                        ^
1 error
```

Given the compilation error above, which fix would resolve the error?

⚠ Common exam trap

It's easy for candidates to think adding a `throws` clause (Option A) is always the correct fix, but the question's context (a compilation error within a method that may not be allowed to throw checked exceptions, such as the main method in a simple program) makes try-catch the appropriate solution, and they overlook that `FileInputStream` (Option D) also throws the same checked exception.

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

Wrap the code in a try-catch block for FileNotFoundException.

The compilation error indicates that the code uses a constructor or method that throws a checked `FileNotFoundException` (a subclass of `IOException`). In Java, checked exceptions must be either caught with a try-catch block or declared in the method signature. Wrapping the code in a try-catch block for `FileNotFoundException` handles the exception locally, resolving the compilation error without altering the method signature.

Answer analysis

Option-by-option breakdown

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

  • Add a throws FileNotFoundException clause to the main method.

    Why it's wrong here

    Adding a throws clause is a valid way to handle a checked exception, but it does not resolve the compilation error if the calling method (e.g., main) must handle the exception internally. In many exam contexts, try-catch is the preferred fix.

  • Change FileReader to BufferedReader.

    Why it's wrong here

    Changing FileReader to BufferedReader does not address the FileNotFoundException because BufferedReader still requires a FileReader or similar that throws the exception.

  • Wrap the code in a try-catch block for FileNotFoundException.

    Why this is correct

    Wrapping the code in a try-catch block for FileNotFoundException correctly handles the checked exception locally, satisfying the compiler's requirement.

  • Use FileInputStream instead.

    Why it's wrong here

    Using FileInputStream would also throw FileNotFoundException, so it does not resolve the compilation error.

Visual reference

Client Recursive Resolver Root DNS (13 root servers) TLD DNS (.com, .org, …) Authoritative example.com query IP addr answer

About these practice questions

Courseiva writes every 1Z0-811 question from scratch — 481 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-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.