ProgrammingHardCAT · MBA placement
What is printed?
What is printed?
d = dict.fromkeys(['a', 'b'], []) d['a'].append(1) print(d, d['b'] is d['a'])
- A. {'a': [1], 'b': []} False
- B. {'a': [1], 'b': [1]} False
- C. {'a': [1], 'b': [1]} True
- D. {'a': [], 'b': [1]} True
Correct answer: C. {'a': [1], 'b': [1]} True
dict.fromkeys stores the same list object as the value for every key. Appending through one key therefore changes the value seen through the other key.
More Python Programming questions
- What is printed? python def pc(x): c = 0 while x: x &= x - 1 c += 1 return c print(pc(0b1011011), pc(255), pc(0))
- What is printed? python def rev8(x): r = 0 for in range(8): r = (r << 1) | (x & 1) x >>= 1 return r print(rev8(1), rev8(0b00010110)…
- What is printed? python def maj(a): cand, cnt = None, 0 for x in a: if cnt == 0: cand = x cnt += 1 if x == cand else -1 return cand…
- What is printed? python print(list(dict.fromkeys([3, 1, 3, 2, 1])), sorted(set([3, 1, 3, 2, 1]))[::-1])
- What is printed? python def sortcount(a): if len(a) <= 1: return a, 0 m = len(a) // 2 l, x = sortcount(a[:m]) r, y = sortcount(a[m:]) out…
- What is printed? python print([10, 20][True], [10, 20][False])