PCPP1 practice question 7 of 10

Select the true statements about the json.-dumps () function. (Select two answers.)

Full set: 45 questions
Answer options
A: It returns a JSON string. Correct answer
B: It returns a Python entity.
C: It takes a JSON string as its argument
D: It takes Python data as its argument. Correct answer
Correct Answer:
A. It returns a JSON string.; D. It takes Python data as its argument.
Explanation

The json.dumps() function is used for serialization, which is the process of converting a Python object into a JSON formatted string. The function's name itself, "dump string," indicates its purpose. It takes a Python data structure (like a dictionary or list) as its primary argument and returns a str object representing that data in JSON format. This process is essential for storing data or transmitting it over a network in a standardized, language-independent format.

Why Incorrect

B. It returns a Python entity. This describes the deserialization function json.loads(), which parses a JSON string and converts it back into a Python object.
C. It takes a JSON string as its argument. This is also a description of json.loads(), which requires a JSON-formatted string as input for deserialization.

References

1. Official Python Documentation: The documentation for the json module explicitly states, "Serialize obj to a JSON formatted str using this conversion table." This confirms that the function takes a Python object (obj) as an argument (D) and returns a JSON formatted string (A).

Source: Python Software Foundation. (2023). json — JSON encoder and decoder. Python 3.12.0 documentation. Retrieved from https://docs.python.org/3/library/json.html#json.dumps, Section: json.dumps.

2. University Courseware (MIT OpenCourseWare): In the lecture notes for "Introduction to Computer Science and Programming in Python," the process of using the json library is detailed. The examples show json.dumps() being called with a Python dictionary as an argument to produce a string, reinforcing that it takes Python data and returns a string.

Source: Guttag, J. V. (2016). Introduction to Computation and Programming Using Python, With Application to Understanding Data (2nd ed.). MIT Press. Chapter 10, Section 10.2, "Reading and Writing Files". (While the book is a primary source, its principles are reflected in MIT OCW course 6.0001 materials which cover file I/O and data structures).

3. University Courseware (University of Michigan): The "Python for Everybody" course materials, widely adopted by universities, explain that json.dumps() is used to "take a Python data structure and put it into a string." This directly supports both correct answers.

Source: Severance, C. R. (2016). Python for Everybody: Exploring Data in Python 3. Chapter 13: Web Services and XML. Section: "Parsing JSON".

Scroll to Top