ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
print(sorted([3, 1, 2], reverse=True) == [3, 2, 1], [3, 1, 2][::-1], sorted('cab')[::-1])- A. False [2, 1, 3] ['c', 'b', 'a']
- B. True [3, 2, 1] ['c', 'b', 'a']
- C. True [2, 1, 3] ['c', 'b', 'a']
- D. True [2, 1, 3] ['a', 'b', 'c']
Correct answer: C. True [2, 1, 3] ['c', 'b', 'a']
Sorting in descending order gives [3, 2, 1], so the comparison holds. Slicing with [::-1] reverses the list as it is, not sorted, and sorted('cab') is ['a', 'b', 'c'] before reversing.
More Python Programming questions
- What is printed? python print(int(str(7) 3) 2 % 10, 111 2, (11 2) 10 + 1)
- What is printed? python n = float('nan') print(n in [n], float('nan') in [n], n == n, [n] == [n])
- What is printed? python print(0.1 + 0.2 in [0.3], 0.3 in [0.1 + 0.2], round(0.1 + 0.2, 1) in [0.3])
- What is printed? python print(1 in {1.0: 'a'}, '1' in {1: 'a'}, 1.0 in {True: 1}, {1, True, 1.0})
- What is printed? python d = {} d[1.0] = 'x' d[1] = 'y' print(d, len(d), d[True])
- What is printed? python print(sorted(['b', 'a', 'c'], key=lambda s: -ord(s)))