ProgrammingHardProduct-company MCQ pattern

What is printed on the last line?

What is printed on the last line?
try:
    sorted([1, '2'])
except TypeError:
    print('TypeError')
print(sorted([1, '2'], key=str))
  1. A. [1, '3']
  2. B. [1, '2']
  3. C. ['2', 1]
  4. D. [1, '2', 2]

Correct answer: B. [1, '2']

Sorting mixed int and str without a key raises TypeError. With key=str both items become text, so '1' sorts before '2' and the original objects are kept in the output.

More Python Programming questions

Exams that ask this topic