ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
words = ["banana", "Apple", "cherry", "apple"]
print(sorted(words), sorted(words, key=str.lower)[:2], max(words, key=len))
  1. A. ['apple', 'Apple', 'banana', 'cherry'] ['Apple', 'apple'] banana
  2. B. ['Apple', 'apple', 'banana', 'cherry'] ['Apple', 'apple'] banana
  3. C. ['Apple', 'banana', 'cherry', 'apple'] ['Apple', 'apple'] banana
  4. D. ['Apple', 'apple', 'banana', 'cherry'] ['apple', 'Apple'] cherry

Correct answer: B. ['Apple', 'apple', 'banana', 'cherry'] ['Apple', 'apple'] banana

Default string order compares code points, and every uppercase letter comes before every lowercase one, so 'Apple' sorts first. With key=str.lower, 'Apple' and 'apple' tie and the stable sort keeps their original order. 'banana' and 'cherry' both have length 6; max returns the first maximum.

More Python Programming questions

Exams that ask this topic