Courseiva
Exceptions and File I/O →mediumMultiple Select

PCAP Exceptions and File I/O Practice Question

Which TWO of the following are true about the 'with' statement in Python file I/O?

⚠ Common exam trap

Python Institute often tests the misconception that the 'with' statement is only for file I/O, but the trap here is that candidates forget the context manager protocol requires both __enter__ and __exit__ methods, not just one, and that it applies to any object implementing that protocol.

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

✓

It is equivalent to a try/finally block

The 'with' statement in Python is designed to simplify exception handling by encapsulating the setup and teardown of a resource in a context manager, which is functionally equivalent to a try/finally block. When you use 'with', the context manager's __exit__ method is guaranteed to be called even if an exception occurs, ensuring that cleanup actions like closing a file are performed, just as a finally clause would.

Answer analysis

Option-by-option breakdown

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

  • ✗

    It can only be used with file objects

    Why it's wrong here

    The with statement is not restricted to file objects; it works with any object that implements the context manager protocol by defining both __enter__ and __exit__ methods. This includes locks (threading.Lock), network sockets, decimal contexts, and contextlib.redirect_stdout. The misconception likely arises because files are the most common use case, but the protocol is a general-purpose language feature.

  • ✓

    It is equivalent to a try/finally block

    Why this is correct

    The with statement is semantically equivalent to a try/finally block because the interpreter guarantees that __exit__ is invoked when the block ends, whether it finishes normally or an exception is raised. This ensures cleanup code always runs, just as finally does after try. The key difference is that with also calls __enter__ at the start to initialize the resource, making it a structured way to pair setup and teardown.

  • ✓

    It automatically closes the file after the block

    Why this is correct

    For file objects, the automatic closing behavior is implemented by the file object's __exit__ method, which internally calls the file's close() method when the with block exits. This means the file is closed without an explicit close() invocation, preventing resource leaks even if an error occurs inside the block. It is this built-in cleanup that makes with the preferred idiom for file handling in Python.

  • ✗

    It does not require an __exit__ method

    Why it's wrong here

    The with statement fundamentally requires the object to provide both __enter__ and __exit__; __exit__ is the method responsible for performing cleanup after the block finishes. If __exit__ is missing, the Python interpreter raises an AttributeError when attempting to use the object in a with statement, as it explicitly searches for that method during evaluation. Without __exit__, there is no mechanism to release resources or handle exceptions, so the statement would be meaningless.

  • ✗

    It requires the object to have an __enter__ method only

    Why it's wrong here

    Providing only __enter__ is insufficient because the with statement needs __exit__ to manage teardown and handle any exceptions raised inside the block. At runtime, Python calls __enter__ first, then binds its return value to the as target, and finally calls __exit__ with exception details (or None values) when the block completes. An object with only __enter__ will cause a TypeError when used with with, because the protocol demands that both methods exist for the statement to work correctly.

About these practice questions

This PCAP question is part of Courseiva's 421-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.