ProgrammingModerateTCS NQT · Infosys
What is printed on the first line?
What is printed on the first line?
print(sorted(['10', '9', '2'], key=int))
print(sorted(['10', '9', '2']))
print(sorted(['a', 'B', 'c']))
print(sorted('aA'))- A. ['10', '2', '9']
- B. ['2', '9', '10']
- C. ['10', '9', '2']
- D. ['2', '10', '9']
Correct answer: B. ['2', '9', '10']
key=int compares the numeric values 2, 9 and 10. Without a key the strings are compared character by character, which puts '10' first and gives the trap value ['10', '2', '9'].
More Python Programming questions
- What is printed? python print('a' 'b' 2, ('a' 'b') 2, 'ab' 0 == '', 'x' 3 + 'y' 2)
- What is printed? python print('%.2f' % 2.675, '%.2f' % 2.665, round(2.665, 2), format(2.5, '.0f'), format(0.125, '.2f'))
- What is printed? python print(0.1 + 0.2 + 0.3 == 0.6, 0.3 + 0.2 + 0.1 == 0.6, 0.1 + (0.2 + 0.3) == 0.6)
- What is printed? python from decimal import Decimal print(Decimal('0.1') + Decimal('0.2') == Decimal('0.3'), Decimal(0.1) ==…
- What is printed? python from fractions import Fraction print(Fraction(1, 3) + Fraction(1, 6), Fraction(0.5), Fraction('0.1') +…
- What is printed? python print(int('0b101', 0), int('0o17', 8), int('z', 36), int('-0'), int('+7'))