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
Using with ensures proper acquisition and release of resources.
- ✗
open('config.json', 'r') as f:\n data = f.read()
Why it's wrong here
Missing the 'with' keyword; syntax error.
- ✗
with open('config.json', 'r') as f, data = f.read()
Why it's wrong here
Syntax error; cannot assign variable inside with statement like that.
- ✗
file = open('config.json', 'r')\ndata = file.read()\nfile.close()
Why it's wrong here
This manually closes the file but does not guarantee closure if an exception occurs before file.close().
Go deeper
Related to this question
About these practice questions
This 200-901 question is part of Courseiva's 989-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 →
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.