Courseiva
Exceptions and File I/OmediumMultiple ChoiceObjective-mapped

PCAP Exceptions and File I/O Practice Question

Exhibit

with open('file.txt', 'r') as f:
    data = f.read()
    print(f.closed)
# Output: False

Refer to the exhibit. What is the output of the code?

⚠ Common exam trap

The trap is that candidates may assume all exceptions are caught, but here the except clause does not match the raised exception, leading to an unhandled error. An unhandled exception causes a runtime error, not a silent output.

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

Error

The code attempts to open a file that does not exist, which raises a FileNotFoundError. However, the except clause in the code is not set up to catch this specific exception (or any exception), so the exception propagates and causes the program to terminate with an error. Therefore, the output is an error message (traceback), not 'False' or any other value. Option A is correct.

Answer analysis

Option-by-option breakdown

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

  • Error

    Why this is correct

    Error is the correct outcome because the code attempts to open a non-existent file inside the try block, which raises a FileNotFoundError (a subclass of OSError). The except clause, as written, does not match this exception type—probably catching a different exception or being absent—so the exception is left unhandled. Python's interpreter then prints a traceback describing the error and stops execution, meaning no normal output is produced and the program terminates with an error status.

  • True

    Why it's wrong here

    True would be the output only if the try block executed successfully and the code reached an explicit print(True) or an expression evaluating to True. But when the file cannot be found, the open() call raises an exception before any such print statement is encountered. Because the except clause fails to catch the raised exception, the program never continues to the line that would output True; instead, the unhandled exception triggers a traceback and the program errors out.

  • False

    Why it's wrong here

    False would require the program to reach a print(False) or an expression that evaluates to False, typically after a successful operation or after the exception is caught by a matching handler. Here, the missing-file exception is not caught by the except clause, so the normal flow of execution is interrupted and jumps straight to the interpreter's error reporting. Consequently, the user sees a traceback and an error message, not the literal output False.

  • None

    Why it's wrong here

    None could be displayed if the code executed a print statement with no argument or printed the result of a function that returns None, such as print(None) or print(some_none_returning_func()). In this scenario, the try block raises an unhandled exception—the file does not exist—and the except clause does not handle it, so the execution never reaches any print statement. The interpreter instead outputs an exception traceback, which is an error, not the Python value None.

About these practice questions

This PCAP question is part of Courseiva's 169-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 PCAP practice question is part of Courseiva's free Python Institute 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 PCAP exam.