ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
from collections import ChainMap
defaults = {"color": "red", "size": 1}
user = {"size": 3}
cm = ChainMap(user, defaults)
cm["color"] = "blue"
print(cm["size"], defaults["color"], user, len(cm))
  1. A. 3 red {'size': 3, 'color': 'blue'} 2
  2. B. 3 blue {'size': 3} 2
  3. C. 1 red {'size': 3, 'color': 'blue'} 2
  4. D. 3 red {'size': 3, 'color': 'blue'} 3

Correct answer: A. 3 red {'size': 3, 'color': 'blue'} 2

Lookups search the maps in order, so size comes from user. Writes always go to the first map, so 'color' is added to user and defaults is untouched. len counts distinct keys across maps.

More Python Programming questions

Exams that ask this topic