Python Tuples & Unpacking Quiz
A 35-question quiz covering tuple creation, immutability, access, operations, packing/unpacking, and value swapping.
Question 1
Which of the following syntaxes correctly creates a tuple containing a single element, the integer 5?
Question 2
Consider the following code snippet defining a tuple. What is the primary characteristic that distinguishes this structure from a Python list?
my_data = (10, 20, 30)Question 3
When you assign multiple values to a single variable separated by commas without brackets, what data type does Python automatically create?
values = 1, 2, 3Question 4
Analyze the code below. What will happen when the interpreter attempts to execute the second line?
coordinates = (10, 20)
coordinates[0] = 15Question 5
Which of the following statements accurately describes the result of the tuple unpacking operation shown below?
point = (3, 4)
x, y = pointQuestion 6
You have two variables, a = 5 and b = 10. Which Python idiom correctly swaps their values using tuple packing and unpacking?
Question 7
Given the tuple `colors = ('red', 'green', 'blue')`, what value is retrieved by the expression `colors[-1]`?
Question 8
What does the `count()` method return when called on a tuple object?
Question 9
Analyze the following code. What will be the content of the variable `rest` after execution?
numbers = (1, 2, 3, 4, 5)
first, *rest = numbersQuestion 10
Why might a developer choose to use a tuple instead of a list for a specific collection of data?
Question 11
What is the result of attempting to delete a single element from a tuple using `del my_tuple[0]`?
Question 12
Consider the nested tuple structure below. How would you correctly access the string 'target'?
data = (1, (2, 3), ('target', 4))Question 13
What happens if you try to unpack a tuple into fewer variables than there are elements, without using a starred expression?
t = (1, 2, 3)
a, b = tQuestion 14
Which method would you use to find the position of the first occurrence of the value 10 in a tuple?
Question 15
Analyze the code below. Is the tuple `my_tuple` considered modified after the operation?
my_tuple = (1, [2, 3])
my_tuple[1].append(4)Question 16
What is the correct way to concatenate two tuples, `t1` and `t2`, into a new tuple?
Question 17
Evaluate the following expression. What is the length of the resulting tuple?
t = (1, 2) * 3Question 18
Which of the following is a valid use case for an empty tuple?
Question 19
What is the output of the following code snippet involving tuple slicing?
letters = ('a', 'b', 'c', 'd', 'e')
print(letters[1:4])Question 20
How does Python handle the comparison of two tuples, such as `(1, 2, 3) < (1, 2, 4)`?
Question 21
In the context of function arguments, what does the syntax `*args` in a function definition represent?
Question 22
What happens when you convert a list to a tuple using `tuple([1, 2, 3])`?
Question 23
Analyze the code below. What is the type of the variable `result`?
result = 1,Question 24
Which of the following operations is NOT supported by tuples?
Question 25
What is the result of the following unpacking operation?
a, b, c = "XYZ"Question 26
Consider the code below. What will be printed to the console?
t = (10, 20, 30)
print(t.index(20))Question 27
Why is the following code valid even though tuples are immutable?
t = (1, 2)
t = (3, 4)Question 28
What is the output of the following code using the `in` operator?
menu = ('pizza', 'pasta', 'salad')
print('burger' in menu)Question 29
How can you create a tuple from a range of numbers, specifically 0 to 4?
Question 30
What is the result of `len(())`?
Question 31
Which of the following is true regarding using tuples as dictionary keys?
Question 32
What is the output of the following nested unpacking?
data = ("John", (25, "Engineer"))
name, (age, job) = data
print(job)Question 33
What happens if you use the `*` operator on the right side of an assignment during tuple creation?
t = (*[1, 2], 3)Question 34
Analyze the code below. What is the value of `y`?
x = 10
y = 20
x, y = y, x + 10Question 35
Which built-in function would you use to combine two tuples of the same length into a sequence of pairs?
