Courseiva
Exceptions and File I/O →hardMultiple Choice

PCAP Exceptions and File I/O Practice Question

A programmer writes a context manager to manage a temporary file. The class implements __enter__ to open the file and return the handle, and __exit__ to close it. During the with body, an exception is raised. The programmer wants the exception to propagate after cleanup. What should __exit__ return in this case?

⚠ Common exam trap

The trap here is assuming that __exit__ returning the file handle is a valid cleanup pattern, when any truthy return value suppresses the 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

✓

False

The __exit__ return value is a suppression flag: a falsy result lets the exception propagate, while a truthy result suppresses it. Since the goal is to clean up the temporary file and still surface the error, returning False is correct. Returning a handle or re-raising inside __exit__ would either hide the exception or replace it with a different one.

Answer analysis

Option-by-option breakdown

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

  • ✗

    True

    Why it's wrong here

    Returning True from __exit__ tells Python that the exception was handled and should be suppressed. If the programmer returns True, the with statement swallows the exception and execution continues after the block, which contradicts the stated goal of letting the exception propagate. Cleanup still happens, but the error is silently hidden, which is rarely the desired behavior for a temporary file manager.

  • ✗

    The file handle returned by open()

    Why it's wrong here

    The return value of __exit__ is interpreted as a truthiness flag for exception suppression, not as a file handle. Returning the handle is truthy, so Python would suppress the exception, which is the opposite of what the programmer wants. The handle should be closed inside __exit__, not returned, and the method should return False or None to let the exception propagate.

  • ✗

    None, only after re-raising the exception with raise

    Why it's wrong here

    Returning None already prevents suppression, so re-raising inside __exit__ is unnecessary and would produce a second exception during cleanup. If __exit__ itself raises, that new exception replaces the original one, obscuring the root cause. The intended pattern is to close the file and return a falsy value so the original exception continues to propagate naturally.

  • ✓

    False

    Why this is correct

    The __exit__ method receives the exception type, value, and traceback, and its return value controls suppression. Returning False (or a falsy value such as None) means the exception is not suppressed and will propagate after __exit__ finishes. In this scenario the programmer wants the exception to propagate, so returning False after closing the file satisfies the requirement while still performing cleanup.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.