ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
d = {1: 'a'}
d[1.0] = 'b'
d[True] = 'c'
print(d)
  1. A. {1: 'a', 1.0: 'b', True: 'c'}
  2. B. {True: 'c'}
  3. C. {1: 'a'}
  4. D. {1: 'c'}

Correct answer: D. {1: 'c'}

1, 1.0 and True are equal and hash the same, so they are one key. The dict keeps the first key object and only updates the value, so the output is {1: 'c'}.

More Python Programming questions

Exams that ask this topic