Bird
Raised Fist0

What will be the output of the following code?

medium📝 Predict Output Q13 of Q15
Python - Structured Data Files
What will be the output of the following code?
import json
py_data = {'name': 'Alice', 'age': 30}
json_str = json.dumps(py_data)
print(type(json_str))
A<class 'dict'>
BTypeError
C<class 'list'>
D<class 'str'>
Step-by-Step Solution
Solution:
  1. Step 1: Understand what json.dumps() returns

    The json.dumps() function converts Python data into a JSON string, so the result is a string type.
  2. Step 2: Check the printed type

    The type(json_str) will be <class 'str'> because json_str holds a JSON string.
  3. Final Answer:

    <class 'str'> -> Option D
  4. Quick Check:

    dumps() output type = str [OK]
Quick Trick: dumps() returns a string, so type is str [OK]
Common Mistakes:
MISTAKES
  • Thinking dumps() returns a dict
  • Confusing dumps() with loads()
  • Expecting a list type output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes