ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
print(sorted(['Bb', 'aA', 'bb', 'AA'], key=str.lower))
  1. A. ['aA', 'bb', 'AA', 'Bb']
  2. B. ['aA', 'AA', 'Bb', 'bb']
  3. C. ['Bb', 'bb', 'aA', 'AA']
  4. D. ['aA', 'bb', 'Bb', 'AA']

Correct answer: B. ['aA', 'AA', 'Bb', 'bb']

Sorting by the lowercase key puts aA and AA first (both key 'aa'), then Bb and bb. sorted is stable, so ties keep their input order: aA came before AA, and Bb came before bb.

More Python Programming questions

Exams that ask this topic