Bird
0
0

Find the mistake in this code:

medium📝 Debug Q7 of 15
Python - Structured Data Files
Find the mistake in this code:
import json
my_dict = {'a': 1, 'b': 2}
json_string = json.dumps(my_dict, indent=2, sortkeys=True)
print(json_string)
AThe indent value must be a string, not an integer.
BThe parameter name should be sort_keys, not sortkeys.
CThe dictionary keys must be integers.
Djson.dumps cannot be assigned to a variable.
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter spelling

    The correct parameter name is sort_keys with an underscore, not sortkeys.
  2. Step 2: Validate other code parts

    Indent is correctly an integer, dictionary keys can be strings, and json.dumps returns a string that can be assigned.
  3. Final Answer:

    The parameter name should be sort_keys, not sortkeys. -> Option B
  4. Quick Check:

    Parameter is sort_keys with underscore [OK]
Quick Trick: Use sort_keys with underscore, not sortkeys [OK]
Common Mistakes:
  • Misspelling sort_keys parameter
  • Passing indent as string
  • Thinking json.dumps can't be assigned

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes