Lists & Tuples · Question 9 · Medium
One name is a slice copy of the list a and another is a plain assignment. What is printed?
a = [1, 2, 3]
b = a[:]
b.append(4)
c = a
c.append(5)
print(a, b)
- [1, 2, 3] [1, 2, 3, 4]
- [1, 2, 3, 5] [1, 2, 3, 4]
- [1, 2, 3, 4, 5] [1, 2, 3, 4, 5]
- [1, 2, 3, 5] [1, 2, 3, 4, 5]
Answer and explanation
Answer: B. [1, 2, 3, 5] [1, 2, 3, 4]
Explanation: a[:] builds a new list, so appending 4 to b does not touch a. c = a only gives the same list a second name, so appending 5 through c shows up in a.
Previous: The same list goes through sorted() and then sort(). What does this print? · Next: remove() and pop() are used on a queue of token numbers. What does this print?
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
- An element of a tuple is assigned a new value. What happens?
- Two variables are exchanged using tuple packing and unpacking. What is printed?
- The same list goes through sorted() and then sort(). What does this print?
- 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…
- The list a is extended once with + and once with += through a second name…
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