Python Tuples & Unpacking Quiz

Python
0 Passed
0% acceptance

A 35-question quiz covering tuple creation, immutability, access, operations, packing/unpacking, and value swapping.

35 Questions
~70 minutes
1

Question 1

Which of the following syntaxes correctly creates a tuple containing a single element, the integer 5?

A
(5,)
B
(5)
C
[5]
D
tuple(5)
2

Question 2

Consider the following code snippet defining a tuple. What is the primary characteristic that distinguishes this structure from a Python list?

python
my_data = (10, 20, 30)
A
It is immutable, meaning its elements cannot be changed after creation.
B
It can only store integers, whereas lists can store any type.
C
It is unordered, so elements cannot be accessed by index.
D
It has a dynamic size and can grow automatically.
3

Question 3

When you assign multiple values to a single variable separated by commas without brackets, what data type does Python automatically create?

python
values = 1, 2, 3
A
A tuple
B
A list
C
A set
D
A string
4

Question 4

Analyze the code below. What will happen when the interpreter attempts to execute the second line?

python
coordinates = (10, 20)
coordinates[0] = 15
A
A TypeError will be raised because tuples do not support item assignment.
B
The first element will be successfully updated to 15.
C
A new tuple (15, 20) will be created and assigned to coordinates.
D
The code will run but the value will remain 10.
5

Question 5

Which of the following statements accurately describes the result of the tuple unpacking operation shown below?

python
point = (3, 4)
x, y = point
A
Variable x is assigned 3, and variable y is assigned 4.
B
Variable x is assigned (3, 4), and y is undefined.
C
Variable x is assigned 3, and y is assigned the remaining tuple.
D
An error occurs because the tuple must be converted to a list first.
6

Question 6

You have two variables, a = 5 and b = 10. Which Python idiom correctly swaps their values using tuple packing and unpacking?

A
a, b = b, a
B
swap(a, b)
C
a = b; b = a
D
(a, b) = swap(b, a)
7

Question 7

Given the tuple `colors = ('red', 'green', 'blue')`, what value is retrieved by the expression `colors[-1]`?

A
'blue'
B
'green'
C
'red'
D
IndexError
8

Question 8

What does the `count()` method return when called on a tuple object?

A
The number of times a specified value appears in the tuple.
B
The total number of elements in the tuple.
C
The index of the first occurrence of a value.
D
A dictionary of element frequencies.
9

Question 9

Analyze the following code. What will be the content of the variable `rest` after execution?

python
numbers = (1, 2, 3, 4, 5)
first, *rest = numbers
A
[2, 3, 4, 5]
B
(2, 3, 4, 5)
C
2
D
5
10

Question 10

Why might a developer choose to use a tuple instead of a list for a specific collection of data?

A
To ensure the data remains constant and cannot be accidentally modified.
B
Because tuples are always faster to sort than lists.
C
Because tuples support more built-in methods than lists.
D
To allow for dynamic resizing of the collection.
11

Question 11

What is the result of attempting to delete a single element from a tuple using `del my_tuple[0]`?

A
TypeError
B
The element is removed and the tuple shrinks.
C
The element is replaced with None.
D
The tuple is deleted entirely.
12

Question 12

Consider the nested tuple structure below. How would you correctly access the string 'target'?

python
data = (1, (2, 3), ('target', 4))
A
data[2][0]
B
data[2][1]
C
data[1][0]
D
data[3][0]
13

Question 13

What happens if you try to unpack a tuple into fewer variables than there are elements, without using a starred expression?

python
t = (1, 2, 3)
a, b = t
A
ValueError: too many values to unpack
B
The first two values are assigned, and the third is ignored.
C
The last value is assigned to b, and a gets the rest.
D
IndexError
14

Question 14

Which method would you use to find the position of the first occurrence of the value 10 in a tuple?

A
index(10)
B
find(10)
C
search(10)
D
locate(10)
15

Question 15

Analyze the code below. Is the tuple `my_tuple` considered modified after the operation?

python
my_tuple = (1, [2, 3])
my_tuple[1].append(4)
A
Yes, the list inside the tuple is modified, but the tuple structure itself remains the same.
B
No, a TypeError is raised because tuples are immutable.
C
Yes, the tuple is completely recreated in memory.
D
No, the operation is ignored.
16

Question 16

What is the correct way to concatenate two tuples, `t1` and `t2`, into a new tuple?

A
t1 + t2
B
t1.append(t2)
C
t1.extend(t2)
D
merge(t1, t2)
17

Question 17

Evaluate the following expression. What is the length of the resulting tuple?

python
t = (1, 2) * 3
A
6
B
2
C
3
D
5
18

Question 18

Which of the following is a valid use case for an empty tuple?

A
Acting as a placeholder or default value where a sequence is expected.
B
Storing data that will be populated later.
C
Creating a dynamic buffer for I/O operations.
D
Reserving memory for a future list.
19

Question 19

What is the output of the following code snippet involving tuple slicing?

python
letters = ('a', 'b', 'c', 'd', 'e')
print(letters[1:4])
A
('b', 'c', 'd')
B
('a', 'b', 'c')
C
('b', 'c', 'd', 'e')
D
('c', 'd', 'e')
20

Question 20

How does Python handle the comparison of two tuples, such as `(1, 2, 3) < (1, 2, 4)`?

A
It compares elements element-by-element until a difference is found.
B
It compares the sum of the elements.
C
It compares the length of the tuples.
D
It raises a TypeError.
21

Question 21

In the context of function arguments, what does the syntax `*args` in a function definition represent?

A
It collects extra positional arguments into a tuple.
B
It collects extra keyword arguments into a dictionary.
C
It forces the function to accept only a list.
D
It unpacks a tuple passed by the caller.
22

Question 22

What happens when you convert a list to a tuple using `tuple([1, 2, 3])`?

A
A new immutable tuple containing the list's elements is created.
B
The list is modified in place to become immutable.
C
A reference to the list is stored in a tuple wrapper.
D
An error occurs because lists cannot be converted.
23

Question 23

Analyze the code below. What is the type of the variable `result`?

python
result = 1,
A
tuple
B
int
C
list
D
string
24

Question 24

Which of the following operations is NOT supported by tuples?

A
sort()
B
len()
C
min()
D
max()
25

Question 25

What is the result of the following unpacking operation?

python
a, b, c = "XYZ"
A
a='X', b='Y', c='Z'
B
a='XYZ', b='XYZ', c='XYZ'
C
ValueError
D
a='X', b='Y', c=None
26

Question 26

Consider the code below. What will be printed to the console?

python
t = (10, 20, 30)
print(t.index(20))
A
1
B
20
C
2
D
True
27

Question 27

Why is the following code valid even though tuples are immutable?

python
t = (1, 2)
t = (3, 4)
A
Because the variable 't' is being reassigned to a completely new tuple object.
B
Because the tuples are small enough to be mutable.
C
It is actually invalid and will raise an error.
D
Because Python optimizes small integer tuples.
28

Question 28

What is the output of the following code using the `in` operator?

python
menu = ('pizza', 'pasta', 'salad')
print('burger' in menu)
A
False
B
True
C
None
D
Error
29

Question 29

How can you create a tuple from a range of numbers, specifically 0 to 4?

A
tuple(range(5))
B
range(5).to_tuple()
C
(range(5))
D
[0..4].tuple()
30

Question 30

What is the result of `len(())`?

A
0
B
1
C
None
D
Error
31

Question 31

Which of the following is true regarding using tuples as dictionary keys?

A
Tuples can be keys if all their elements are also immutable.
B
Tuples can never be dictionary keys.
C
Tuples can always be keys, regardless of content.
D
Only empty tuples can be keys.
32

Question 32

What is the output of the following nested unpacking?

python
data = ("John", (25, "Engineer"))
name, (age, job) = data
print(job)
A
Engineer
B
25
C
John
D
(25, 'Engineer')
33

Question 33

What happens if you use the `*` operator on the right side of an assignment during tuple creation?

python
t = (*[1, 2], 3)
A
It creates a flat tuple (1, 2, 3).
B
It creates a nested tuple ([1, 2], 3).
C
It raises a SyntaxError.
D
It creates a list.
34

Question 34

Analyze the code below. What is the value of `y`?

python
x = 10
y = 20
x, y = y, x + 10
A
20
B
30
C
10
D
40
35

Question 35

Which built-in function would you use to combine two tuples of the same length into a sequence of pairs?

A
zip()
B
map()
C
combine()
D
pair()

QUIZZES IN Python