ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
print(sorted([(1, 2), (1,), (0, 9, 9)]))
  1. A. [(1,), (1, 2), (0, 9, 9)]
  2. B. [(0, 9, 9), (1, 2), (1,)]
  3. C. [(1,), (0, 9, 9), (1, 2)]
  4. D. [(0, 9, 9), (1,), (1, 2)]

Correct answer: D. [(0, 9, 9), (1,), (1, 2)]

Tuples compare element by element, and a shorter tuple that is a prefix of a longer one sorts first. (0, 9, 9) comes first because 0 is smallest, and (1,) sorts before (1, 2).

More Python Programming questions

Exams that ask this topic