🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Python Dictionaries: all 15 questions

Quiz: Python Dictionaries · Question 12 of 15

What does d["z"] do if the key "z" is not in the dictionary d?

What does d["z"] do if the key "z" is not in the dictionary d?

  1. A.Returns None
  2. B.Returns 0
  3. C.Raises KeyError
  4. D.Adds "z" with the value None
Answer & explanation

Correct answer: Raises KeyError

Square brackets need the key to exist. Use d.get("z") to get None instead of an error.

Can you get the rest right without peeking?

Take the timed Quiz: Python Dictionaries quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →