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))- A. 3 red {'size': 3, 'color': 'blue'} 2
- B. 3 blue {'size': 3} 2
- C. 1 red {'size': 3, 'color': 'blue'} 2
- 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
- What is printed? python import bisect grades = "FDCBA" cut = [60, 70, 80, 90] print([grades[bisect.bisect(cut, s)] for s in [59, 60, 85…
- What is printed? python x = [1, 2, 3] y = [x, x] y[0] = y[0] + [4] y[1] += [5] print(x, y)
- What is printed? python def f(a, b=[]): b = b + [a] f.defaults = (b,) return b print(f(1), f(2), f(3, []), f(4))
- What is printed? python it = (x x for x in range(5)) first = next(it) rest = [v for v in it if v % 2 == 0] again = sum(1 for in it)…
- What is printed? python def g(): for i in range(3): try: yield i finally: print(f"f{i}", end=" ") for v in g(): if v == 1: break…
- What is printed? python def deco(cls): orig = cls.init def init(self, a): orig(self, a) self.ok = True cls.init = init return cls @deco…