ProgrammingHardProduct-company MCQ pattern

What is printed?

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

Correct answer: D. ['A', 'b', 'B', 'c'] ['A', 'B', 'b', 'c']

With key=str.upper, the two b's compare equal and keep their input order, so b comes before B. Without a key, uppercase letters sort before lowercase, giving A, B, b, c.

More Python Programming questions

Exams that ask this topic