ProgrammingModerateTCS NQT · Infosys

What is printed?

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

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

Tuples sort by their first item and then their second, so the sorted list is (a,1), (a,2), (b,1), and slicing from index 1 drops (a,1). dict items sorted give pairs in key order.

More Python Programming questions

Exams that ask this topic