ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
print({k % 4: k * k for k in range(9, -1, -1)})- A. {1: 1, 0: 0, 3: 9, 2: 4}
- B. {0: 0, 1: 1, 2: 4, 3: 9}
- C. {1: 81, 0: 64, 3: 49, 2: 36}
- D. {1: 1, 0: 0, 3: 49, 2: 36}
Correct answer: A. {1: 1, 0: 0, 3: 9, 2: 4}
A dict keeps the position of the first insertion of each key, but its value is the last one written. Iterating from 9 down, keys 1, 0, 3, 2 appear in that order, and the last k written to key 3 is k = 3, giving 9.
More Python Programming questions
- What is printed? python print(sum([0.1] 10) == 1.0, 0.1 3 == 0.3)
- What is printed? python import heapq iv = sorted([(0, 30), (5, 10), (15, 20)]) h = [] for s, e in iv: if h and h[0] <= s…
- What is printed? python from collections import Counter a = [1, -1, 2, -2, 0] cnt = Counter({0: 1}) s = 0 ans = 0 for x in a: s += x ans…
- What is printed? python print(4 & 3 == 0, 6 & 3 == 0)
- What is printed? python print(int(1e23), int(1e20))
- What is printed? python x = [3, 1, 2].sort() print(x, sorted([3, 1, 2]))