Tuples — Immutable Sequences & Unpacking — Concept Check
Questions on Tuples — Immutable Sequences & Unpacking. Tuples are immutable, ordered sequences — used for fixed records, multiple return values, dict keys, and elegant unpacking.
Questions in this quiz
8 questions on Tuples — Immutable Sequences & Unpacking — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer tries to use a tuple as a key in a dictionary. What must be true about the tuple's contents?
- AThe tuple must be empty.
- BAll elements must be immutable.
- CThe tuple must contain only strings.
- DAll elements must be integers.
2.Given the code: `t = (1, [2, 3]); t[1].append(4)`, what will `print(t)` output?
- ATypeError
- B(1, [2, 3, 4])
- C(1, [2, 3, 4, 4])
- D(1, [2, 3])
3.A developer uses a tuple to store coordinates as dictionary keys. What advantage does this provide?
- ATuples are hashable, enabling use as dict keys.
- BTuples are mutable, allowing easy updates.
- CTuples are faster to access than lists.
- DTuples automatically sort the coordinates.
4.What will happen if you try to assign a new value to an element in a tuple?
- AA TypeError will be raised.
- BThe operation will be ignored.
- CThe tuple will update silently.
- DThe tuple will convert to a list.
5.What is the output of `print((2, 3) in visited)` given `visited[(2, 3)] = True`?
- ATrue
- BFalse
- CTypeError
- DNone
6.Why might a developer choose a namedtuple over a regular tuple?
- ANamedtuples are faster than regular tuples.
- BNamedtuples consume less memory.
- CNamedtuples provide readable field names.
- DNamedtuples are mutable and allow changes.
7.In Python, what is the result of unpacking a tuple `point = (3, 4)` with `x, y = point`?
- Ax = 4, y = 3
- BTypeError
- Cx = (3, 4), y = None
- Dx = 3, y = 4
8.A developer mistakenly believes that tuples are faster than lists in all operations. What is a correct statement?
- ATuples are always faster than lists.
- BTuples are faster only in creation and iteration.
- CTuples and lists have the same speed.
- DLists are faster for indexing.