ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
print({k: v for k, v in zip(['a', 'b', 'a'], [1, 2, 3])})- A. {'a': 1, 'b': 2, 'a': 3}
- B. {'a': 1, 'b': 2}
- C. {'a': 3, 'b': 2}
- D. KeyError
Correct answer: C. {'a': 3, 'b': 2}
zip pairs up ('a',1), ('b',2), ('a',3). A dict cannot hold two entries with the same key, so the later pair for 'a' (value 3) overwrites the earlier one.
More Python Programming questions
- What is printed? python matrix = [[1, 2, 3], [4, 5, 6]] print([[row[i] for row in matrix] for i in range(3)])
- What is printed? python print({x % 3 for x in range(10)})
- What is printed? python def countdown(n): while n > 0: yield n n -= 1 g = countdown(3) print(list(g)) print(list(g))
- What is printed? python it = iter([1, 2, 3]) print(next(it), next(it)) for x in it: print("loop", x) print(list(it))
- What is printed? python def flatten(lst): for item in lst: if isinstance(item, list): yield from flatten(item) else: yield item…
- What is printed? python g = (x for x in range(3)) print(sum(g), sum(g))