ProgrammingVery hardCAT · MBA placement

What is printed?

What is printed?
d = {'b': 2, 'a': 2, 'c': 1}
print(sorted(d.items(), key=lambda kv: (-kv[1], kv[0])), sorted(d, key=d.get))
  1. A. [('a', 2), ('b', 2), ('c', 1)] ['c', 'a', 'b']
  2. B. [('b', 2), ('a', 2), ('c', 1)] ['c', 'b', 'a']
  3. C. [('c', 1), ('a', 2), ('b', 2)] ['c', 'b', 'a']
  4. D. [('a', 2), ('b', 2), ('c', 1)] ['c', 'b', 'a']

Correct answer: D. [('a', 2), ('b', 2), ('c', 1)] ['c', 'b', 'a']

The first sort orders by negative value and then by key. The second sorts the keys by value, and ties keep dictionary insertion order, so b comes before a.

More Python Programming questions

Exams that ask this topic