Lists & Tuples · Question 8 · Medium
The same list goes through sorted() and then sort(). What does this print?
a = [3, 1, 2]
b = sorted(a)
c = a.sort()
print(a, b, c)
- [1, 2, 3] [1, 2, 3] None
- [3, 1, 2] [1, 2, 3] [1, 2, 3]
- [1, 2, 3] [1, 2, 3] [1, 2, 3]
- [3, 1, 2] [1, 2, 3] None
Answer and explanation
Answer: A. [1, 2, 3] [1, 2, 3] None
Explanation: sorted(a) returns a new sorted list and leaves a alone. a.sort() then sorts a in place and returns None, so c is None while a and b are both [1, 2, 3].
Previous: Two variables are exchanged using tuple packing and unpacking. What is printed? · Next: One name is a slice copy of the list a and another is a plain assignment. What…
Practise lists & tuples
Try a fresh set of lists & tuples questions with an explanation after every answer, free and without signing up: Lists & Tuples practice. Or sit a full placement mock test.
More lists & tuples questions
- A list is appended to another list. What length does this print?
- An element of a tuple is assigned a new value. What happens?
- Two variables are exchanged using tuple packing and unpacking. What is printed?
- One name is a slice copy of the list a and another is a plain assignment. What…
- remove() and pop() are used on a queue of token numbers. What does this print?
- A starred name is used while unpacking a list. What is printed?
- For the tuple t = (1, 2, 3), which of these operations is NOT allowed?
- A marks table is stored as a list of rows. What does this print?
- Kavya has a list a and wants b to be a separate list with the same items, so…
- A two-element slice of a list is replaced by a one-element list. What is…
More python practice
Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators
LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge