PCAP Exceptions and File I/O Practice Question
Exhibit
Traceback (most recent call last):
File "app.py", line 9, in <module>
with open("config.json", "r") as f:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "app.py", line 11, in <module>
config = json.load(f)
^^^^^^^^^^^
NameError: name 'json' is not definedRefer to the exhibit. A developer ran the script and saw the above traceback. The intended behavior was to load a JSON configuration file, and if the file is missing, create a default config. What is the most likely root cause of the second exception (NameError)?
⚠ Common exam trap
Python Institute often tests the distinction between file I/O exceptions (like FileNotFoundError) and name resolution errors (NameError), trapping candidates who focus on the file handling part of the traceback rather than recognizing that the second exception is about an undefined module name.
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
✓
The script did not import the json module.
The traceback shows a NameError for 'json.loads', which indicates that the name 'json' is not defined in the current namespace. This occurs when the script attempts to call json.loads() without first importing the json module. The intended behavior of loading a JSON configuration file requires the json module to parse the file content, and its absence causes the NameError exception.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The variable 'f' was not defined due to the FileNotFoundError.
Why it's wrong here
The FileNotFoundError from open() would have stopped execution before reaching json.load(f), so f would never be the cause of a NameError. In the actual traceback, the NameError explicitly says name 'json' is not defined, meaning the interpreter evaluated json first and failed there; f is defined if open succeeded. Even if the file were missing, the first exception would be FileNotFoundError, not a NameError about json. Thus, the root cause is not f being undefined.
- ✓
The script did not import the json module.
Why this is correct
NameError: name 'json' is not defined means Python could not find a binding for the name 'json' in any accessible scope. The json module is part of the standard library but is not automatically loaded; it must be brought into scope with an explicit import json statement. Since the script calls json.load(f) without having imported json, the name lookup fails at runtime. This is the classic missing-import error and is unrelated to the file's existence, content, or open mode.
- ✗
The config.json file exists but is empty.
Why it's wrong here
If config.json existed but were empty, json.load(f) would raise a json.JSONDecodeError because an empty file is not valid JSON. The traceback's first exception is FileNotFoundError, which proves the file is absent; an empty file would still be found by open() and would not produce that error. Additionally, a JSONDecodeError would occur inside the json module only after the name 'json' has already been resolved, so it could never manifest as a NameError.
- ✗
The file was opened in binary mode instead of text mode.
Why it's wrong here
The file-open mode has no effect on module imports, so using binary mode could not cause a NameError for 'json'. If the script had used 'rb', json.load would still require the json name to be defined; a missing import would produce the same NameError regardless of mode. Since the traceback shows FileNotFoundError before any reading happens, mode-specific behavior never even gets a chance to run, and changing the mode would not fix the missing import.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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 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.