ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
print(sorted(['apple', 'Banana', 'cherry'], key=str.lower), sorted(['apple', 'Banana', 'cherry']))
  1. A. ['Banana', 'apple', 'cherry'] ['apple', 'Banana', 'cherry']
  2. B. ['apple', 'Banana', 'cherry'] ['Banana', 'apple', 'cherry']
  3. C. ['apple', 'Banana', 'cherry'] ['apple', 'Banana', 'cherry']
  4. D. ['Banana', 'apple', 'cherry'] ['Banana', 'apple', 'cherry']

Correct answer: B. ['apple', 'Banana', 'cherry'] ['Banana', 'apple', 'cherry']

With key=str.lower, banana sorts after apple but before cherry. Without a key, the uppercase B (code point 66) sorts before every lowercase letter, so Banana comes first.

More Python Programming questions

Exams that ask this topic