Bird
Raised Fist0

What is the output of this code?

medium📝 Predict Output Q4 of Q15
Python - Structured Data Files
What is the output of this code?
import json
data = {'a': 1, 'b': 2}
print(json.dumps(data, indent=2, sort_keys=True))
A{"a":1,"b":2}
B{ "b": 2, "a": 1 }
C{ "a": 1, "b": 2 }
D{ "a": 1, "b": 2 }
Step-by-Step Solution
Solution:
  1. Step 1: Understand indent=2 and sort_keys=True

    The indent=2 adds 2 spaces indentation per level. sort_keys=True sorts keys alphabetically, so 'a' comes before 'b'.
  2. Step 2: Analyze output format

    The output will be pretty printed with keys sorted: 'a' first, then 'b', each key-value pair on its own indented line.
  3. Final Answer:

    { "a": 1, "b": 2 } -> Option C
  4. Quick Check:

    indent=2 + sort_keys=True formats keys alphabetically with 2-space indent [OK]
Quick Trick: sort_keys=True sorts keys; indent=2 adds 2 spaces [OK]
Common Mistakes:
MISTAKES
  • Ignoring sort_keys effect on key order
  • Confusing indent spacing
  • Expecting compact JSON without spaces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes