Courseiva
Question 305 of 169
Exceptions and File I/OmediumDrag & DropObjective-mapped

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.

Drag steps to the numbered slots on the right, or tap a step then tap a slot.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4
5Step 5

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().

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 11, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.