Courseiva
Software Development and DesignhardMultiple ChoiceObjective-mapped

200-901 Software Development and Design Practice Question

A developer is writing a Python script that processes a large CSV file. The script uses 'with open(file, 'r') as f' to read the file. Why is this approach preferred over calling f = open(file) and then f.close()?

⚠ Common exam trap

Cisco often tests the distinction between a language feature (context managers) and unrelated performance or mode characteristics, so candidates may incorrectly associate 'with' with speed or chunking rather than its core purpose of guaranteed cleanup.

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 automatically closes the file when the block exits, even if an exception is raised.

The 'with' statement in Python implements a context manager that automatically calls the file object's __exit__ method when the block exits, ensuring the file is closed even if an exception occurs. This prevents resource leaks and is the recommended pattern for file I/O in Python, as explicitly calling f.close() may be skipped if an exception is raised before the close call.

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 automatically closes the file when the block exits, even if an exception is raised.

    Why this is correct

    The context manager guarantees cleanup, which is the main advantage.

  • It allows the file to be opened in both read and write mode simultaneously.

    Why it's wrong here

    The mode is specified; 'r' is read-only.

  • It reduces memory usage by loading the file in chunks.

    Why it's wrong here

    The 'with' statement does not automatically chunk the file; it just manages the context.

  • It increases the speed of file operations.

    Why it's wrong here

    Performance is not significantly different.

About these practice questions

One of 989 original 200-901 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 200-901 practice question is part of Courseiva's free Cisco 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 200-901 exam.