Dictionaries & Sets · Question 2 · Easy

get() is called for a key that is not in the dictionary, with and without a default. What does this print?

d = {"a": 1}
print(d.get("b"), d.get("b", 0))
  1. KeyError
  2. 0 0
  3. None 0
  4. None None
Answer and explanation

Answer: C. None 0

Explanation: get() never raises KeyError. For a missing key it returns None, or the second argument if one is given, so the output is None 0.

Previous: A stationery shop updates its stock dictionary. What is printed? · Next: The same key is written twice in a dictionary literal. What is printed?

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge