ProgrammingHardTCS NQT · Wipro pattern

What is printed?

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

Correct answer: B. ['A', 'B', 'a', 'b'] ['A', 'a', 'b', 'B']

A plain sort compares code points, so uppercase letters come first. With key=str.lower, equal keys keep their input order, so A comes before a and b before B.

More Python Programming questions

Exams that ask this topic