Editor

Python tuple MCQ questions with answers

Immutability, packing and unpacking, and the tuple that holds a list.

0 of 20 answered

  1. 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

    len() 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.

    Run this code in the editor
  2. 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 y, x is packed into the tuple (7, 3) before anything is assigned, then unpacked into x and y. That is why the swap needs no temporary variable.

    Run this code in the editor
  3. Question 3 · Easy

    What is the output of this code?

    t = (1, 2) + (3,)
    print(t * 2)
    Show the answer

    Answer: C

    + joins two tuples into (1, 2, 3), and * 2 repeats its items, giving a new flat tuple of six items. It does not multiply each number.

    Run this code in the editor
  4. 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

    count(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.

    Run this code in the editor
  5. Question 5 · Easy

    What is the output of this code?

    letters = tuple('abc')
    print(letters, len(letters))
    Show the answer

    Answer: B

    tuple() 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',).

    Run this code in the editor
  6. 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 TypeError. To change an item, build a new tuple, for example ('Sun',) + days[1:].

    Run this code in the editor
  7. 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, 2 is a tuple too.

  8. 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.

  9. 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 editor
  10. Question 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. t[1].append(4) changes that list in place, so printing the tuple shows the longer list.

    Run this code in the editor
  11. 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 (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.

    Run this code in the editor
  12. 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 t[1:2] is the one-item tuple (20,), printed with its comma. Indexing with t[1] returns the item itself, 20.

    Run this code in the editor
  13. 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, 1, 2 is a tuple, so points[1, 2] looks up the key (1, 2). Tuples can be dictionary keys because they are immutable and hashable.

    Run this code in the editor
  14. 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 ValueError; a, *b = (1, 2, 3) would work.

    Run this code in the editor
  15. 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]) raises TypeError and lists cannot be keys.

  16. 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() or sorted() method because they cannot change. The built-in sorted() accepts a tuple but always returns a list, so wrapping it in tuple() gives a sorted tuple.

  17. 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

    t[1] += [4] runs in two steps: the list's += extends it in place, then Python tries to store the result back into t[1]. That store fails with TypeError, but the list has already changed.

    Run this code in the editor
  18. Question 18 · Hard

    What is the output of this code?

    *head, tail = (7,)
    print(head, tail)
    Show the answer

    Answer: A

    tail takes the only value, so nothing is left for head. A starred target can collect zero values, and it is then an empty list, never None or a tuple.

    Run this code in the editor
  19. 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 TypeError. (1, (2, 3)) would work as a key.

    Run this code in the editor
  20. 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