Bird
0
0

Find the bug in this code snippet:

medium📝 Debug Q7 of 15
Python - Structured Data Files

Find the bug in this code snippet:

import json

my_dict = {'a': 1, 'b': 2}
json_str = json.dump(my_dict)
print(json_str)

Ajson.dump() requires a file object as second argument
Bjson.dumps() should be used instead of json.dump()
Cprint statement is incorrect
DNo bug
Step-by-Step Solution
Solution:
  1. Step 1: Understand json.dump() usage

    json.dump() writes JSON to a file and requires a file object as second argument.
  2. Step 2: Identify missing file argument

    The code calls json.dump(my_dict) without file, causing error.
  3. Final Answer:

    json.dump() requires a file object as second argument -> Option A
  4. Quick Check:

    json.dump() needs file argument, else error [OK]
Quick Trick: Use json.dumps() for string, json.dump() for file [OK]
Common Mistakes:
  • Using dump without file
  • Confusing dump and dumps

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes