Python tuple MCQ questions with answers
Immutability, packing and unpacking, and the tuple that holds a list.
0 of 20 answered
Question 1 · Easy
What is the output of this code?
t = (1, 2, (3, 4), [5, 6]) print(len(t))
Show the answer
Answer: B
Run this code in the editorlen()counts the top-level items: 1, 2, the tuple(3, 4)and the list[5, 6]. The items inside the nested tuple and list are not counted separately.Question 2 · Easy
What is the output of this code?
x, y = 3, 7 x, y = y, x print(x, y)
Show the answer
Answer: A
The right side
Run this code in the editory, xis packed into the tuple(7, 3)before anything is assigned, then unpacked intoxandy. That is why the swap needs no temporary variable.Question 3 · Easy
What is the output of this code?
t = (1, 2) + (3,) print(t * 2)
Show the answer
Answer: C
Run this code in the editor+joins two tuples into(1, 2, 3), and* 2repeats its items, giving a new flat tuple of six items. It does not multiply each number.Question 4 · Easy
What is the output of this code?
t = (6, 1, 6, 6, 3) print(t.count(6), t.index(3))
Show the answer
Answer: D
Run this code in the editorcount(6)returns how many times 6 appears, which is 3.index(3)returns the position of the first 3, and positions start at 0, so it is 4.Question 5 · Easy
What is the output of this code?
letters = tuple('abc') print(letters, len(letters))
Show the answer
Answer: B
Run this code in the editortuple()makes one item from each element of an iterable, and iterating over a string gives its characters. For a one-item tuple holding the whole string, write('abc',).Question 6 · Easy
Which error does this code raise?
days = ('Mon', 'Tue', 'Wed') days[0] = 'Sun'
Show the answer
Answer: B, TypeError: 'tuple' object does not support item assignment
Tuples are immutable, so assigning to an index raises
Run this code in the editorTypeError. To change an item, build a new tuple, for example('Sun',) + days[1:].Question 7 · Easy
Which statement about tuples is true?
Show the answer
Answer: B, Once created, a tuple cannot have items added, removed or replaced
Tuples are immutable: no method or assignment changes their items. They can mix types freely, and the commas, not the parentheses, make a tuple:
u = 1, 2is a tuple too.Question 8 · Easy
Which line creates a tuple holding only the string
'red'?Show the answer
Answer: A
The trailing comma makes a one-item tuple.
('red')is a string inside grouping parentheses,tuple('red')splits the string into three characters, and square brackets make a list.Question 9 · Medium
What is the output of this code?
first, *middle, last = (1, 2, 3, 4, 5) print(middle)
Show the answer
Answer: C
The starred name collects every value the other names do not take, here 2, 3 and 4. A starred target is always a list, even when the value being unpacked is a tuple.
Run this code in the editorQuestion 10 · Medium
What is the output of this code?
t = (1, [2, 3]) t[1].append(4) print(t)
Show the answer
Answer: D
A tuple cannot change which objects it holds, but the list inside it is still mutable.
Run this code in the editort[1].append(4)changes that list in place, so printing the tuple shows the longer list.Question 11 · Medium
What is the output of this code?
print((1, 2, 3) < (1, 3), (2,) > (1, 9, 9))
Show the answer
Answer: A
Tuples compare item by item from the left, and the first difference decides. In
Run this code in the editor(1, 2, 3) < (1, 3)the second items give 2 < 3, so the result is True and length does not matter. In the second comparison 2 > 1 decides at once.Question 12 · Medium
What is the output of this code?
t = (10, 20, 30, 40) print(t[1:2], t[1])
Show the answer
Answer: B
Slicing a tuple always returns a tuple, so
Run this code in the editort[1:2]is the one-item tuple(20,), printed with its comma. Indexing witht[1]returns the item itself, 20.Question 13 · Medium
What is the output of this code?
points = {(0, 0): 'origin', (1, 2): 'A'} print(points[1, 2])
Show the answer
Answer: C
Inside square brackets,
Run this code in the editor1, 2is a tuple, sopoints[1, 2]looks up the key(1, 2). Tuples can be dictionary keys because they are immutable and hashable.Question 14 · Medium
Which error does this code raise?
a, b = (1, 2, 3)
Show the answer
Answer: C, ValueError: too many values to unpack (expected 2)
Unpacking needs exactly as many names as values unless one name is starred. Three values into two names raises
Run this code in the editorValueError;a, *b = (1, 2, 3)would work.Question 15 · Medium
Why can a tuple of numbers be a dictionary key when a list cannot?
Show the answer
Answer: C, A tuple of immutable items is hashable; a list is mutable and never hashable
Dictionaries find keys by their hash, which must never change. A tuple of immutable items can be hashed; a list can change, so
hash([1, 2])raisesTypeErrorand lists cannot be keys.Question 16 · Medium
Which line sorts
t = (3, 1, 2)and keeps the result a tuple?Show the answer
Answer: D
Tuples have no
sort()orsorted()method because they cannot change. The built-insorted()accepts a tuple but always returns a list, so wrapping it intuple()gives a sorted tuple.Question 17 · Hard
What is the output of this code?
t = (1, [2, 3]) try: t[1] += [4] except TypeError: print('error') print(t)
Show the answer
Answer: D
Run this code in the editort[1] += [4]runs in two steps: the list's+=extends it in place, then Python tries to store the result back intot[1]. That store fails withTypeError, but the list has already changed.Question 18 · Hard
What is the output of this code?
*head, tail = (7,) print(head, tail)
Show the answer
Answer: A
Run this code in the editortailtakes the only value, so nothing is left forhead. A starred target can collect zero values, and it is then an empty list, neverNoneor a tuple.Question 19 · Hard
Which error does this code raise?
seen = {} key = (1, [2, 3]) seen[key] = 'x'
Show the answer
Answer: A, TypeError: unhashable type: 'list'
A tuple is hashable only when every item in it is hashable. This tuple holds a list, so hashing it for the dictionary key raises
Run this code in the editorTypeError.(1, (2, 3))would work as a key.Question 20 · Hard
What does
squares = (n * n for n in range(3))create?Show the answer
Answer: D, A generator object that produces 0, 1 and 4 when iterated
Parentheses around a comprehension make a generator expression, not a tuple: Python has no tuple comprehension. Wrap it in
tuple(...)to get(0, 1, 4).
Keep going
- Read the lesson behind these questions: Tuples in the PythonBook
- Next topic: Python dictionary MCQ questions with answers
- Before this: Python list MCQ questions with answers
- Write the code yourself in the Python coding challenges, which check your solution with tests.