Context Manager Question Explained
This worked Python Fundamentals practice question tests Files. Try the answer first, then review why A is the best choice.
Short answer
The correct answer is A. It automatically closes the file when the block exits.
The with statement uses a context manager. For files, it ensures the file is closed even if an error occurs. The key is to match the exact action in the prompt to the tool, command, or concept that performs that action directly. Here, A. It automatically closes the file when the block exits is the only option that satisfies the scenario without adding unrelated behavior.
The Question
Why use with open("file.txt") as f?
Why A is correct
The with statement uses a context manager. For files, it ensures the file is closed even if an error occurs. The key is to match the exact action in the prompt to the tool, command, or concept that performs that action directly. Here, A. It automatically closes the file when the block exits is the only option that satisfies the scenario without adding unrelated behavior.
Why the other options are wrong
Option B does not match the requirement in the prompt. The correct answer is A because The with statement uses a context manager. For files, it ensures the file is closed even if an error occurs.
Option C does not match the requirement in the prompt. The correct answer is A because The with statement uses a context manager. For files, it ensures the file is closed even if an error occurs.
Option D does not match the requirement in the prompt. The correct answer is A because The with statement uses a context manager. For files, it ensures the file is closed even if an error occurs.
Key idea: Files
Files questions test whether you can connect a common workflow problem to the right practical command, service, or concept. Instead of memorizing isolated definitions, look for the job being done in the prompt: staging a file, querying rows, running code, persisting container data, checking a service, or choosing the right cloud building block. The best answer usually names the simplest tool that performs that job directly. For Python Fundamentals, the safest study pattern is to read the scenario, underline the action verb, and eliminate answers that solve a different problem. If an option sounds related but changes the scope, adds risk, or describes a different layer of the stack, it is probably a distractor. After choosing an answer, explain why the other three fail; that habit builds the practical judgment these quizzes are meant to measure.
Ready to test the full skill?
Take the free Python Fundamentals quiz and find the exact basics to review next. No signup required.
Practice related questions
Same skill area or adjacent concept. Use these after reviewing the explanation.