ProgrammingModerateTCS NQT · Infosys

What is printed on the first line?

What is printed on the first line?
print(sorted(['10', '9', '2'], key=int))
print(sorted(['10', '9', '2']))
print(sorted(['a', 'B', 'c']))
print(sorted('aA'))
  1. A. ['10', '2', '9']
  2. B. ['2', '9', '10']
  3. C. ['10', '9', '2']
  4. D. ['2', '10', '9']

Correct answer: B. ['2', '9', '10']

key=int compares the numeric values 2, 9 and 10. Without a key the strings are compared character by character, which puts '10' first and gives the trap value ['10', '2', '9'].

More Python Programming questions

Exams that ask this topic