ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted(['b', 'A', 'c', 'B'], key=str.casefold, reverse=True))
- A. ['c', 'b', 'A', 'B']
- B. ['A', 'b', 'B', 'c']
- C. ['c', 'b', 'B', 'A']
- D. ['c', 'A', 'b', 'B']
Correct answer: C. ['c', 'b', 'B', 'A']
Casefolded keys are b, a, c, b, so the descending order is c, then the two b's, then a. reverse=True keeps equal keys in their original order, so b (input index 0) comes before B (index 3).
More Python Programming questions
- What is printed? python try: print(sorted([3, None, 1])) except TypeError: print('cannot compare')
- What is printed? python print('%(a)s-%(b)05.1f' % {'a': 'x', 'b': 3.14159})
- What is printed? python s = 'Ünïcödé' print(len(s), len(s.encode()), s.upper() == 'ÜNÏCÖDÉ')
- What is printed? python print(int('10', 2), int('0o17', 0), int('17', 8), int('0xff', 16))
- What is printed? python x = 0xDEAD print(x >> 8 & 0xFF, (x >> 8) & 0xF, hex(x & -x))
- What is printed? python print(sorted([-3, 2, -1, 4], key=lambda v: (abs(v) % 3, v)))