Drag steps to the numbered slots on the right, or tap a step then tap a slot.
PCAP Exceptions and File I/O Practice Question
Drag and drop the steps to serialize a Python object to JSON using the json module into the correct order.
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
Step 1: Import the json module. Step 2: Create a Python dictionary. Step 3: Open a file in write mode. Step 4: Use json.dump() to write the dictionary to the file.
Serialization to JSON involves importing json, preparing data, using dumps for string or dump for file output.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
Step 1: Import the json module. Step 2: Create a Python dictionary. Step 3: Open a file in write mode. Step 4: Use json.dump() to write the dictionary to the file.
Why this is correct
This sequence is correct because the json module must be imported before any of its functions, such as json.dump(), can be referenced. The Python dictionary must then exist as the data to be serialized, followed by opening a file in write mode to provide a writable file-like object. Only after the file is successfully opened can json.dump() be called, as it writes the serialized JSON directly to that open handle. Attempting to run json.dump() without an open file would raise a TypeError or AttributeError, so the given order ensures every dependency is satisfied before the critical call.
- ✗
Step 1: Create a Python dictionary. Step 2: Import the json module. Step 3: Open a file in write mode. Step 4: Use json.dump() to write the dictionary to the file.
Why it's wrong here
This is incorrect because importing the module should be done before any other operations to ensure the module is available; creating the dictionary first is not a syntax error but is not the conventional order.
- ✗
Step 1: Import the json module. Step 2: Open a file in write mode. Step 3: Create a Python dictionary. Step 4: Use json.dump() to write the dictionary to the file.
Why it's wrong here
This is incorrect because you need to have the dictionary object ready before calling json.dump(); opening the file before creating the object is not logically wrong but is not the typical sequence and could lead to confusion.
- ✗
Step 1: Import the json module. Step 2: Create a Python dictionary. Step 3: Use json.dump() to write the dictionary to the file. Step 4: Open a file in write mode.
Why it's wrong here
This order is invalid because json.dump() is called before the target file is opened, even though the file is opened afterward. At the time json.dump() executes, the variable intended to hold the file object either does not exist (causing a NameError) or refers to something that is not a writable file (causing a TypeError). Opening the file after the dump call cannot retroactively provide the required writable stream, so serialization fails outright. The correct approach is to open the file in write mode first, then pass that open file object as the second argument to json.dump().
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 →
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.