ProgrammingModerateTCS NQT · Wipro pattern
What is printed?
What is printed?
from collections import defaultdict
d = defaultdict(list)
d['a'].append(1)
print('b' in d, d['b'], len(d), 'b' in d)- A. False [] 1 False
- B. True [] 2 True
- C. False [] 2 True
- D. False [] 2 False
Correct answer: C. False [] 2 True
The first membership test runs before 'b' is created, so it is False. Reading d['b'] creates the key with an empty list, so len is 2 and the last test is True.
More Python Programming questions
- What is printed? python m = [[1, 2, 3], [4, 5, 6]] rot = [list(r) for r in zip(m[::-1])] print(rot)
- What is printed? python def hist(h): st = [] best = 0 h = h + [0] for i, x in enumerate(h): start = i while st and st[-1][1] >= x: idx…
- What is printed? python def knap(items, cap): dp = [0] (cap + 1) for w, v in items: for c in range(cap, w - 1, -1): dp[c] = max(dp[c]…
- What is printed? python from collections import deque def knight(src, dst): seen = {src: 0} q = deque([src]) moves = [(1, 2), (2, 1), (-1…
- For an array of n = 10^5 values, which approach counts the distinct values in every window of size k = n / 2 in the least total time?
- What is printed? python a = [1, 2, 3, 4] try: print(a[len(a) / 2]) except TypeError: print('TypeError') print(a[len(a) // 2])