ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
d = {}
d[1] = 'a'
d[1.0] = 'b'
d[True] = 'c'
print(d)- A. {1: 'c'}
- B. {True: 'c'}
- C. {1: 'a'}
- D. {1: 'a', 1.0: 'b', True: 'c'}
Correct answer: A. {1: 'c'}
1, 1.0 and True are equal and have the same hash, so they are the SAME dict key. The first key object (1) is kept and its value is overwritten twice, ending as 'c'.
More Python Programming questions
- What is printed? python x = [1, 2, 3] for i in x: x.remove(i) print(x)
- What is printed? python fs = [lambda: i for i in range(3)] print([f() for f in fs])
- What is printed? python def f(x, lst=[]): lst.append(x) return lst a = f(1) b = f(2, []) c = f(3) print(a, b, c)
- What is printed? python def f(): try: return 1 finally: return 2 print(f())
- What is printed? python print(1 < 2 > 1.5, 3 > 2 == 2, (3 > 2) == 2)
- What is printed? python a = [1, 2, 3] b = a a = a + [4] b += [5] print(a, b)