ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
from collections import defaultdict
d = defaultdict(list)
d["a"].append(1)
_ = d["b"]
print("c" in d, d.get("c"), len(d), dict(d))
  1. A. False [] 3 {'a': [1], 'b': [], 'c': []}
  2. B. False None 1 {'a': [1]}
  3. C. False None 2 {'a': [1], 'b': []}
  4. D. True None 3 {'a': [1], 'b': [], 'c': []}

Correct answer: C. False None 2 {'a': [1], 'b': []}

Only d[key] (via __missing__) creates a default entry. Reading d['b'] creates 'b'; `in` and .get() never trigger the factory, so 'c' is never added.

More Python Programming questions

Exams that ask this topic