Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Python - Structured Data Files
Identify the error in this code snippet:
import json
py_obj = {'name': 'Bob', 'age': 25}
json_str = json.dumps(py_obj)
py_obj2 = json.loads(json_str)
print(py_obj2['agee'])
ASyntaxError in dictionary
BKeyError due to wrong key 'agee'
CTypeError in json.dumps()
DNo error, prints 25
Step-by-Step Solution
Solution:
  1. Step 1: Check dictionary keys

    The original dictionary has key 'age', but the code tries to access 'agee' which does not exist.
  2. Step 2: Understand error type

    Accessing a non-existent key in a dictionary raises a KeyError.
  3. Final Answer:

    KeyError due to wrong key 'agee' -> Option B
  4. Quick Check:

    Wrong key access = KeyError [OK]
Quick Trick: Check key names carefully to avoid KeyError [OK]
Common Mistakes:
  • Assuming typo keys work
  • Thinking dumps or loads cause error here
  • Ignoring KeyError exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes