Courseiva

200-901 Software Development and Design Practice Question

A developer writes a Python script to read a configuration file. Which code snippet correctly opens the file 'config.json' for reading and ensures the file is closed after use?

⚠ Common exam trap

Cisco often tests the candidate's understanding of Python's context manager (`with` statement) versus manual file handling, expecting candidates to recognize that only the `with` statement guarantees automatic resource cleanup, while explicit `close()` calls are error-prone in the face of exceptions.

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

✓

with open('config.json', 'r') as f:\n data = f.read()

It uses the `with` statement, which is a context manager that automatically calls `f.close()` when the block exits, ensuring the file is properly closed even if an exception occurs. The `'r'` mode opens the file for reading, and `f.read()` reads the entire contents into the `data` variable.

Answer analysis

Option-by-option breakdown

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

  • ✓

    with open('config.json', 'r') as f:\n data = f.read()

    Why this is correct

    The with statement opens config.json in read mode and guarantees closure via context-manager protocol, even if an exception occurs. This satisfies the stem's requirement that the file is closed after use, unlike manual open calls lacking explicit close.

  • ✗

    open('config.json', 'r') as f:\n data = f.read()

    Why it's wrong here

    The snippet omits the with statement, so the file handle is never closed deterministically and relies on garbage collection. It is tempting because the mode and path are correct, but the context manager is what guarantees closure. This form is correct only when the handle is explicitly closed later or kept open deliberately.

  • ✗

    with open('config.json', 'r') as f, data = f.read()

    Why it's wrong here

    The `with` statement requires a colon before its indented block; this snippet uses a comma, so it raises a SyntaxError and never opens the file. The context-manager form is genuinely the right approach for automatic closing, which is what makes the malformed syntax easy to overlook.

  • ✗

    file = open('config.json', 'r')\ndata = file.read()\nfile.close()

    Why it's wrong here

    This works only if `read()` succeeds; an exception between `open()` and `close()` leaves the file descriptor open. It is tempting because explicit `close()` looks deliberate, and it would be acceptable in short scripts where no exception is expected, but it lacks the guaranteed cleanup the question demands.

About these practice questions

This 200-901 question is part of Courseiva's 975-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 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.