Courseiva
Exceptions and File I/O →hardMultiple Select

PCAP Exceptions and File I/O Practice Question

Which THREE of the following are true about the `with` statement for file handling?

⚠ Common exam trap

Python Institute often tests the misconception that `with` is only for files, but the trap here is that candidates may also incorrectly think it can only handle one file at a time, while Python actually supports multiple context managers in a single `with` statement.

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 uses the __enter__ and __exit__ methods of the file object.

The `with` statement relies on the context management protocol, which requires the object to implement the `__enter__` and `__exit__` methods. When a file object is used with `with`, its `__enter__` method returns the file object itself, and its `__exit__` method is called upon block exit to handle cleanup, such as closing the file.

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 files.

    Why it's wrong here

    The `with` statement is a general-purpose context management protocol, not a file-specific feature. Any object that implements the `__enter__` and `__exit__` methods—such as `threading.Lock`, `socket.socket`, or a custom class—can be used in a `with` statement. File objects are merely one common example, so limiting `with` to files would ignore many other valid resource types.

  • ✗

    It can only handle one file at a time.

    Why it's wrong here

    A single `with` statement can manage multiple resources by comma-separating context managers, e.g., `with open('a') as f1, open('b') as f2:` opens two files simultaneously. Alternatively, nested `with` blocks can handle any number of files and other resources. Thus, there is no restriction to one file at a time; the syntax explicitly supports multi-resource acquisition.

  • ✓

    It uses the __enter__ and __exit__ methods of the file object.

    Why this is correct

    When entering a `with` block, Python calls the `__enter__` method on the context manager and assigns its return value to the `as` variable. Upon block exit, it always calls the `__exit__` method, which for a file object performs the actual closing operation. This pair of methods forms the core of the context manager protocol, and the `with` statement is simply a wrapper that guarantees both calls.

  • ✓

    It ensures the file is closed even if an exception occurs inside the block.

    Why this is correct

    The `__exit__` method is invoked regardless of whether the block completes normally, raises an exception, or is exited via `return`, `break`, or `continue`. Even if an unhandled exception propagates out of the block, `__exit__` runs first, so the file is closed before the exception reaches the surrounding code. This provides exception-safe resource cleanup similar to a `try`/`finally` block.

  • ✓

    It automatically closes the file when the block exits.

    Why this is correct

    On normal completion of the `with` block, Python automatically calls `__exit__`, which for a file object closes the underlying file descriptor. This built-in cleanup means you do not need to write an explicit `close()` call, reducing the risk of resource leaks. The file is closed deterministically at the end of the block, before execution continues.

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 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.