Welcome to the Python Tuples Quiz!
Tuples in Python are immutable, ordered collections that can store multiple items of different data types. Unlike lists, tuples cannot be modified after creation, making them useful for storing fixed data. This quiz will test your understanding of Python tuples, covering their properties, methods, and common use cases.
Each question is followed by the correct answer. Good luck!
Table of Contents:
- Python quizzes for beginners series
Tuples Quiz
Quiz Questions
Basic Concepts
- What is a tuple in Python?
a) A mutable list
b) An immutable ordered collection
c) A dictionary
d) A set
Answer
b) An immutable ordered collection
- Which of the following correctly creates a tuple?
a)tup = (1, 2, 3)
b)tup = [1, 2, 3]
c)tup = {1, 2, 3}
d)tup = "1, 2, 3"
Answer
a) tup = (1, 2, 3)
- What will
type((10,))
return?
a)
b)
c)
d)
Answer
b)
- Which method can be used to count occurrences of an element in a tuple?
a)count()
b)index()
c)find()
d)exists()
Answer
a) count()
- How do you access the last element of a tuple
t = (5, 10, 15, 20)
?
a)t[-1]
b)t[3]
c)t[len(t) - 1]
d) All of the above
Answer
d) All of the above
Tuple Properties & Operations
- Can a tuple contain duplicate values?
a) Yes
b) No
Answer
a) Yes
- Which of the following will create an empty tuple?
a)tup = ()
b)tup = tuple()
c)tup = {}
d) Both a and b
Answer
d) Both a and b
- Tuples are faster than lists in Python.
a) True
b) False
Answer
a) True
- What will happen if you try to modify a tuple?
a) It will change the value successfully
b) It will raise aTypeError
c) It will create a new tuple with the modified value
d) It will return None
Answer
b) It will raise a TypeError
- How can you concatenate two tuples
t1 = (1, 2)
andt2 = (3, 4)
?
a)t1 + t2
b)t1.extend(t2)
c)t1.append(t2)
d)concat(t1, t2)
Answer
a) t1 + t2
Tuple Indexing & Slicing
- What is the output of
t = (1, 2, 3, 4, 5)[1:4]
?
a)(2, 3, 4)
b)(1, 2, 3, 4)
c)(2, 3, 4, 5)
d)(3, 4, 5)
Answer
a) (2, 3, 4)
- What happens if you try
t[100]
ont = (10, 20, 30)
?
a) ReturnsNone
b) RaisesIndexError
c) Returns0
d) Prints nothing
Answer
b) Raises IndexError
- What is the result of
t[::-1]
fort = (1, 2, 3, 4)
?
a)(1, 2, 3, 4)
b)(4, 3, 2, 1)
c)(1, 4, 3, 2)
d) Syntax error
Answer
b) (4, 3, 2, 1)
Tuple Methods & Functions
- How do you find the length of a tuple
t = (1, 2, 3, 4, 5)
?
a)size(t)
b)length(t)
c)len(t)
d)count(t)
Answer
c) len(t)
- Which function converts a list into a tuple?
a)list()
b)tuple()
c)set()
d)dict()
Answer
b) tuple()
- How do you check if an element exists in a tuple?
a)exists()
b)in
c)find()
d)contains()
Answer
b) in
Advanced Tuple Concepts
- What is tuple unpacking?
a) Extracting values from a tuple into separate variables
b) Combining two tuples into one
c) Deleting elements from a tuple
d) Sorting a tuple
Answer
a) Extracting values from a tuple into separate variables
- What will the following code output?
t = (1, 2, [3, 4])
t[2][0] = 99
print(t)
a) (1, 2, [99, 4])
b) (1, 2, [3, 4])
c) TypeError
d) (1, 2, 99, 4)
Answer
a) (1, 2, [99, 4])
- Which of the following is true?
a) Tuples use less memory than lists
b) Tuples are faster than lists
c) Tuples are immutable
d) All of the above
Answer
d) All of the above
- Which built-in function allows you to iterate over a tuple while keeping track of the index?
a)enumerate()
b)zip()
c)range()
d)map()
Answer
a) enumerate()
How did you do? π―
- 18-20 correct β π Excellent! Youβre a Python functions pro!
- 14-17 correct β π Great job! Keep practicing.
- 10-13 correct β π Good, but thereβs room for improvement.
- Below 10 β π€ No worries! Review the concepts and try again.