Bird
0
0

What is the output of this code?

medium📝 Predict Output Q13 of 15
Python - Structured Data Files
What is the output of this code?
import json
data = {'name': 'Alice', 'age': 30}
print(json.dumps(data, indent=2))
A{"name": "Alice", "age": 30}
B{ "name": "Alice", "age": 30 }
C{name: Alice, age: 30}
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand json.dumps with indent=2

    The function converts the dictionary to a JSON string with 2 spaces indentation for each nested level.
  2. Step 2: Check the output format

    The output will have new lines and spaces, keys and string values in double quotes, and numeric values as is.
  3. Final Answer:

    { "name": "Alice", "age": 30 } -> Option B
  4. Quick Check:

    Indent=2 adds spaces and new lines [OK]
Quick Trick: Indent adds new lines and spaces for readability [OK]
Common Mistakes:
  • Expecting compact JSON without spaces
  • Using single quotes instead of double quotes
  • Confusing Python dict print with JSON string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes