ProgrammingModerateTCS NQT · Infosys

What is printed?

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

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

Iterating a dict gives its keys, so sorted returns the keys alphabetically, and .values() gives the values, which are then sorted in reverse.

More Python Programming questions

Exams that ask this topic