ProgrammingVery hardTCS NQT · Accenture

What is printed?

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

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

The key is the swapcased letter, so 'A' becomes 'a' and so on. Sorting by these keys compares code points: the uppercase keys A and B (65, 66) come before a and b (97, 98), which gives a, b, A, B.

More Python Programming questions

Exams that ask this topic