ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
print(sorted([3, -3, 2, -2, 1], key=abs))
- A. [-3, -2, 1, 2, 3]
- B. [3, -3, 2, -2, 1]
- C. [1, 2, -2, 3, -3]
- D. [1, 2, -2, 3, -4]
Correct answer: C. [1, 2, -2, 3, -3]
The key abs ties 3 with -3 and 2 with -2. Python's sort is stable, so each tied pair stays in input order, with 3 before -3 and 2 before -2.
More Python Programming questions
- What is printed? python print(f'{-1.5:+.1f}', f'{0.5:.0f}', f'{1.5:.0f}', f'{-0.04:.1f}')
- What is printed? python print(format('x', '>4') + '|' + format(3.14159, '^9.2f') + '|' + format(42, '+') + '|' + format(1234567, ','))
- What is printed? python print('%d%%' % 50, '%x' % 255, '%o' % 8, '%e' % 12345.678)
- What is printed? python print(-(264) >> 3, -(264) // 8, -(264) % 10)
- What is printed? python print('a,b,,c'.split(','), 'a b c'.split(), 'a b c'.split(' '), ' x '.strip(), 'x,'.split(','))
- What is printed? python print(sorted(['Bb', 'aA', 'bb', 'AA'], key=str.lower))