ProgrammingHardGATE CS · Product-company placement

What is printed?

What is printed?
d = {"b": 2, "a": 1}
print(sorted(d.items()), sorted(d, key=d.get), list(reversed(sorted(d.values()))), {**d, "a": 0} == {"a": 0, "b": 2})
  1. A. [('a', 1), ('b', 2)] ['a', 'b'] [2, 1] True
  2. B. [('b', 2), ('a', 1)] ['a', 'b'] [2, 1] True
  3. C. [('a', 1), ('b', 2)] ['b', 'a'] [2, 1] True
  4. D. [('a', 1), ('b', 2)] ['a', 'b'] [1, 2] False

Correct answer: A. [('a', 1), ('b', 2)] ['a', 'b'] [2, 1] True

Sorting items compares the key first. Sorting keys by d.get orders them by value. Dict equality ignores order, and in {**d, 'a': 0} the later 'a' wins.

More Python Programming questions

Exams that ask this topic