ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
print({k % 4: k * k for k in range(9, -1, -1)})
  1. A. {1: 1, 0: 0, 3: 9, 2: 4}
  2. B. {0: 0, 1: 1, 2: 4, 3: 9}
  3. C. {1: 81, 0: 64, 3: 49, 2: 36}
  4. 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

Exams that ask this topic