ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted(['Bb', 'aA', 'bb', 'AA'], key=str.lower))
- A. ['aA', 'bb', 'AA', 'Bb']
- B. ['aA', 'AA', 'Bb', 'bb']
- C. ['Bb', 'bb', 'aA', 'AA']
- D. ['aA', 'bb', 'Bb', 'AA']
Correct answer: B. ['aA', 'AA', 'Bb', 'bb']
Sorting by the lowercase key puts aA and AA first (both key 'aa'), then Bb and bb. sorted is stable, so ties keep their input order: aA came before AA, and Bb came before bb.
More Python Programming questions
- What is printed? python print(-7 % 3, 7 % -3, -7 // 3, 7 // -3)
- What is printed? python print(pow(-7, 3, 5), (-7) 2 % 5, -7 2 % 5)
- What is printed? python print(2100 % 7, pow(2, 100, 7), pow(2, 100, 1))
- What is printed? python x = 1020 print(x // 3, x / 3 == x // 3, x % 3)
- What is printed? python n = int('9' 30) print(len(str(n + 1)), (n + 1) - 1030)
- What is printed? python print(5 & 3, 5 | 3, 5 ^ 3, ~5, 5 << 2, -5 >> 1)