ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
d = {}
d.setdefault('k', []).append(1)
d.setdefault('k', []).append(2)
print(d)- A. {'k': [1, 2]}
- B. {'k': [2]}
- C. {'k': [1]}
- D. {'k': [1, 2], 'k': []}
Correct answer: A. {'k': [1, 2]}
setdefault returns the existing list when the key is present, so both appends go into the same list.
More Python Programming questions
- What is printed? python print(1 / 3 + 1 / 3 + 1 / 3 == 1.0, 0.1 + 0.7)
- What is printed? python print(sorted([(-1, 'b'), (-1, 'a'), (0, 'c')]), max([(1, 'z'), (1, 'a')]))
- What is printed? python x = 0 for i in range(5): x += i if i % 2 else -i print(x)
- What is printed? python it = iter([3, 1, 0, 5, 0]) out = [] for x in iter(lambda: next(it), 0): out.append(x) print(out, list(it))
- What is printed? python s = 'key=value=end' print(s.partition('='), s.rpartition('='), 'a b'.split(' '))
- What is printed? python def shift(s, k): return ''.join(chr((ord(c) - 97 + k) % 26 + 97) if c.islower() else c for c in s)…