ProgrammingEasyTCS NQT · Infosys pattern
What is printed?
What is printed?
nums = [3, 1, 2] r = nums.sort() s = sorted(nums, reverse=True) print(r, nums, s)
- A. None [1, 2, 3] [3, 2, 1]
- B. [1, 2, 3] [1, 2, 3] [3, 2, 1]
- C. None [3, 1, 2] [3, 2, 1]
- D. [1, 2, 3] [3, 1, 2] [3, 2, 1]
Correct answer: A. None [1, 2, 3] [3, 2, 1]
list.sort() sorts in place and returns None. sorted() returns a new list and leaves the original unchanged. Writing nums = nums.sort() is a common bug that loses the list.
More Python Programming questions
- What is printed? python x = 10 def outer(): x = 20 def inner(): nonlocal x x += 1 return x return inner() + x print(outer(), x)
- What is printed? python d = {} d[1] = "a" d[1.0] = "b" d[True] = "c" print(d, len(d))
- What is printed? python a = [1, 2, 3, 4, 5, 6] for x in a: if x % 2 == 0: a.remove(x) print(a)
- What is printed? python print(round(2.5), round(3.5), round(-0.5), int(-2.7), -7 // 2, -7 % 2)
- What is printed? python from collections import Counter c = Counter("mississippi") print(c.mostcommon(2), c["z"], sum(c.values()))
- What is printed? python a, b, c = range(6) x, y = y, x = 1, 2 print(a, b, c, x, y)