PCAP Modules and Packages Practice Question
Exhibit
{
"rules": [
{"action": "allow", "src": "10.0.1.0/24"},
{"action": "deny", "src": "0.0.0.0/0"},
]
}Refer to the exhibit. A Python script uses the following code to load the policy. However, it fails with a JSONDecodeError. What is the most likely cause? ```python
import json
with open('policy.json', 'r') as f:policy = json.load(f) ```
⚠ Common exam trap
Python Institute often tests the subtle difference between Python's permissive syntax (which allows trailing commas) and the strict JSON specification, leading candidates to incorrectly assume that Python's json module would accept trailing commas.
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 JSON file contains a trailing comma after the last element.
Python's json.load() strictly follows the JSON specification (RFC 7159), which does not allow trailing commas after the last element in an array or object. When the JSON file contains a trailing comma, the decoder raises a JSONDecodeError. This is a common syntax error in hand-written or poorly generated JSON files.
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 JSON file contains a trailing comma after the last element.
Why this is correct
The correct diagnosis: Python's json.load() is a strict parser that follows RFC 8259, and a trailing comma after the last element is not valid JSON syntax. The file likely contains something like [{"a":1},{"b":2},], which causes the parser to encounter a comma before the closing bracket and raise JSONDecodeError with a message such as 'Expecting value'. No amount of variable handling or file mode changes can repair that malformed structure.
- ✗
The policy variable is used before assignment elsewhere in the script.
Why it's wrong here
This is wrong because the traceback points to json.load(), meaning the exception is raised inside the JSON parser while it is reading and decoding the file contents. At that point, the Python interpreter has not yet executed any assignment to the 'policy' variable, so a use-before-assignment error elsewhere is irrelevant. An UnboundLocalError would only occur when the script later tries to access 'policy', and it would occur after json.load() returns successfully, not during the parsing step.
- ✗
The file should be opened in binary mode ('rb') instead of text mode.
Why it's wrong here
Opening the file in binary mode ('rb') does not fix or cause a JSONDecodeError. JSON text is valid input passed as either str from a text-mode file or bytes from a binary-mode file, since json.loads() accepts both str and bytes and decodes them according to the UTF-8 default. The error here is a syntax-level violation within the JSON data itself, not a character-encoding or file-open-mode issue, so switching to 'rb' would leave the trailing comma and still raise the same JSONDecodeError.
- ✗
The JSON decoder cannot handle IP addresses in strings.
Why it's wrong here
IP addresses stored as strings in JSON are completely valid—they are simply character sequences inside double quotes, such as "192.168.1.1" or "::1". The JSON decoder parses string values by reading escaped characters and Unicode, and it does not validate or reject textual content like IP addresses. A JSONDecodeError mentioning an IP address would only appear if the address were unquoted (e.g., a bare 192.168.1.1), which is a missing-quotes syntax issue, not a limitation of the decoder.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 421-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 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.