ProgrammingVery hardInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
d = {}
d.setdefault('k', []).append(1)
d.setdefault('k', []).append(2)
print(d, d.setdefault('j', 9), 'j' in d)- A. {'k': [1, 2]} 9 True
- B. {'k': [1, 2], 'j': 9} 9 False
- C. {'k': [1], 'j': 9} 9 True
- D. {'k': [1, 2], 'j': 9} 9 True
Correct answer: D. {'k': [1, 2], 'j': 9} 9 True
print receives the same dict object, and setdefault('j', 9) inserts j before the output is formatted. The dict therefore shows j, and 'j' in d is True.
More Python Programming questions
- What is printed? python w = ['pear', 'fig', 'apple', 'kiwi', 'date'] print(sorted(w, key=lambda s: (len(s), s)), sorted(w, key=len…
- What is printed? python print(bool(0.0), bool(' '), bool(None is None), bool(float('nan')))
- What is printed? python def findmin(a): lo, hi = 0, len(a) - 1 while lo < hi: mid = (lo + hi) // 2 if a[mid] > a[hi]: lo = mid + 1 else…
- What is printed? python g = {'a': [3, 1], 'b': [2]} for v in g.values(): v.sort() print(g, sorted(g.items(), key=lambda kv: len(kv[1])…
- What is printed? python ops = {} for name in ['add', 'mul']: ops[name] = lambda x, y, n=name: x + y if n == 'add' else x y…
- What is printed? python print(list(enumerate('ab', start=1)), list(zip(reversed([1, 2, 3]), 'xyz')))