Welcome to the Python Tuples Manipulation MCQs! Tuples are immutable data structures in Python, commonly used to store collections of items that should not be changed. These questions will assess your understanding of tuple manipulation techniques in Python, including indexing, unpacking, concatenation, and more. Each question is multiple-choice, with only one correct answer. Take your time to carefully read each question and choose the best option. Let’s explore the realm of Python tuples manipulation together!
my_tuple = (1, 2, 3)
print(my_tuple[1])
a) 1
b) 2
c) 3
d) Error: Tuples are immutable
Answer: b
Explanation: Tuples are ordered collections, and indexing starts from 0. So, my_tuple[1]
refers to the element at index 1, which is 2.
a) Concatenation
b) Indexing
c) Appending
d) Slicing
Answer: c
Explanation: Tuples are immutable, so appending elements to a tuple is not valid in Python.
my_tuple = (1, 2, 3, 4, 5)
print(my_tuple[1:4])
a) (1, 2, 3, 4)
b) (2, 3, 4)
c) (2, 3, 4, 5)
d) (1, 2, 3)
Answer: b
Explanation: Slicing a tuple from index 1 to index 4 (exclusive) returns the elements (2, 3, 4).
a) index()
b) find()
c) search()
d) locate()
Answer: a
Explanation: The index()
method is used to find the index of a specified element in a tuple in Python.
my_tuple = ('a', 'b', 'c', 'd', 'e')
print(my_tuple.index('c'))
a) 2
b) 3
c) 4
d) Error: ‘c’ not in tuple
Answer: a
Explanation: The index()
method returns the index of the first occurrence of the specified element in the tuple.
a) count()
b) occurrences()
c) find()
d) search()
Answer: a
Explanation: The count()
method returns the number of occurrences of a specified element in a tuple in Python.
my_tuple = ('a', 'b', 'c', 'a', 'b', 'a')
print(my_tuple.count('a'))
a) 1
b) 2
c) 3
d) 4
Answer: c
Explanation: The count()
method returns the number of occurrences of the specified element in the tuple.
a) reverse()
b) sort()
c) swap()
d) None of the above
Answer: d
Explanation: Tuples are immutable in Python, so there is no built-in method to reverse the elements of a tuple.
my_tuple = (1, 2, 3, 4, 5)
reversed_tuple = my_tuple[::-1]
print(reversed_tuple)
a) (5, 4, 3, 2, 1)
b) (1, 2, 3, 4, 5)
c) (5, 4, 3, 2)
d) Error: Tuples are immutable
Answer: a
Explanation: Slicing with a step of -1 reverses the tuple.
a) extend()
b) concat()
c) join()
d) None of the above
Answer: d
Explanation: Tuples are immutable, so there is no built-in method to concatenate two tuples directly. However, you can use the +
operator to concatenate two tuples.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print(result)
a) (1, 2, 3, 4, 5, 6)
b) (1, 2, 3), (4, 5, 6)
c) (4, 5, 6, 1, 2, 3)
d) Error: Unsupported operand types for +
Answer: a
Explanation: The +
operator concatenates two tuples, resulting in a new tuple containing elements from both tuples.
a) size()
b) length()
c) len()
d) count()
Answer: c
Explanation: The len()
function is used to find the length of a tuple in Python.
my_tuple = (1, 2, 3)
print(len(my_tuple))
a) 1
b) 2
c) 3
d) Error: Missing argument to len()
Answer: c
Explanation: The len()
function returns the number of elements in the tuple.
a) list()
b) to_list()
c) convert_to_list()
d) tuple_to_list()
Answer: a
Explanation: The list()
function is used to convert a tuple into a list in Python.
my_tuple = (1, 2, 3)
converted_list = list(my_tuple)
print(converted_list)
a) [1, 2, 3]
b) (1, 2, 3)
c) [[1, 2, 3]]
d) Error: Unsupported operand types for list()
Answer: a
Explanation: The list()
function converts the tuple my_tuple
into a list.
my_tuple = ('a', 'b', 'c')
my_list = list(my_tuple)
my_list.append('d')
result = tuple(my_list)
print(result)
a) (‘a’, ‘b’, ‘c’, ‘d’)
b) (‘a’, ‘b’, ‘c’)
c) [‘a’, ‘b’, ‘c’, ‘d’]
d) Error: Cannot convert list to tuple
Answer: a
Explanation: The code first converts the tuple to a list, appends ‘d’ to the list, and then converts the list back to a tuple.
a) Tuples are mutable
b) Tuples can contain elements of different data types
c) Tuples support item assignment
d) Tuples are denoted by square brackets []
Answer: b
Explanation: Tuples can contain elements of different data types and are denoted by parentheses (), not square brackets [].
my_tuple = (1, 2, 3)
result = my_tuple * 2
print(result)
a) (1, 2, 3, 1, 2, 3)
b) (2, 4, 6)
c) [1, 2, 3, 1, 2, 3]
d) (2, 4, 6, 1, 2, 3)
Answer: a
Explanation: Multiplying a tuple by an integer replicates the tuple elements.
a) remove()
b) discard()
c) pop()
d) Tuples are immutable, so no such method exists
Answer: d
Explanation: Tuples are immutable in Python, meaning you cannot change, add, or remove elements from a tuple after it has been created.
my_tuple = (1, 2, 3, 4, 5)
result = my_tuple[1:4]
print(result)
a) (1, 2, 3, 4)
b) (2, 3, 4)
c) (2, 3, 4, 5)
d) (1, 2, 3)
Answer: b
Explanation: Slicing a tuple from index 1 to index 4 (exclusive) returns the elements (2, 3, 4).
index()
method do in Python tuples?a) Returns the index of the first occurrence of a specified element
b) Returns the index of the last occurrence of a specified element
c) Adds an element at the specified index
d) Replaces an element at the specified index
Answer: a
Explanation: The index()
method returns the index of the first occurrence of a specified element in the tuple.
my_tuple = ('a', 'b', 'c', 'd', 'e')
result = my_tuple.index('c')
print(result)
a) 2
b) 3
c) 4
d) ‘c’
Answer: a
Explanation: The index()
method returns the index of the first occurrence of the specified element in the tuple.
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple(map(lambda x, y: x + y, tuple1, tuple2))
print(result)
a) (1, 2, 3, 4, 5, 6)
b) (5, 7, 9)
c) (1, 4, 9)
d) Error: Unsupported operand types for +
Answer: b
Explanation: The map()
function applies the lambda function to each corresponding pair of elements from tuple1
and tuple2
, resulting in a tuple of sums.
a) Tuples can be used as keys in dictionaries
b) Tuples can be nested within other tuples
c) Tuples support item assignment after creation
d) Tuples preserve the order of elements
Answer: c
Explanation: Tuples are immutable in Python, so item assignment after creation is not supported.
my_tuple = (1, 2, 3, 4, 5)
result = tuple(filter(lambda x: x % 2 == 0, my_tuple))
print(result)
a) (1, 3, 5)
b) (2, 4)
c) (1, 2, 3, 4, 5)
d) Error: Unsupported operand types for %
Answer: b
Explanation: The filter()
function applies the lambda function to each element in my_tuple
and returns a tuple containing only the elements for which the lambda function returns True.
my_tuple = (1, [2, 3], 4)
my_tuple[1].append(5)
print(my_tuple)
a) (1, [2, 3, 5], 4)
b) (1, [2, 3, 4], 5)
c) (1, [2, 3, 5])
d) Error: Tuples are immutable
Answer: d
Explanation: Although tuples are immutable, the list inside the tuple is mutable, so appending to it will not raise an error.
a) to_dict()
b) dict()
c) convert_to_dict()
d) None of the above
Answer: b
Explanation: The dict()
function can be used to convert a list of tuples into a dictionary.
my_tuple = (('a', 1), ('b', 2), ('c', 3))
result = dict(my_tuple)
print(result)
a) {‘a’: 1, ‘b’: 2, ‘c’: 3}
b) [(‘a’, 1), (‘b’, 2), (‘c’, 3)]
c) ((‘a’, 1), (‘b’, 2), (‘c’, 3))
d) Error: Invalid type for dictionary conversion
Answer: a
Explanation: The dict()
function converts a list of tuples into a dictionary.
my_tuple = (1, 2, 3)
result = my_tuple + (4,)
print(result)
a) (1, 2, 3, 4)
b) (1, 2, 3)
c) (1, 2, 3, 4,)
d) Error: Unsupported operand types for +
Answer: a
Explanation: The +
operator concatenates two tuples, resulting in a new tuple containing elements from both tuples.
a) contains()
b) exists()
c) in operator
d) None of the above
Answer: c
Explanation: The in
operator can be used to check if an element exists in a tuple in Python.
my_tuple = (1, 2, 3)
result = sum(my_tuple)
print(result)
a) 6
b) (1, 2, 3)
c) Error: unsupported operand type for sum
d) Error: ‘tuple’ object has no attribute ‘sum’
Answer: a
Explanation: The sum()
function calculates the sum of all elements in the tuple.
my_tuple = ('apple', 'banana', 'cherry')
result = max(my_tuple)
print(result)
a) ‘banana’
b) ‘cherry’
c) ‘apple’
d) Error: ‘tuple’ object has no attribute ‘max’
Answer: c
Explanation: The max()
function returns the maximum value based on lexicographic order, in this case, ‘cherry’ has the highest lexicographic order.
my_tuple = ('apple', 'banana', 'cherry')
result = sorted(my_tuple)
print(result)
a) [‘apple’, ‘banana’, ‘cherry’]
b) [‘cherry’, ‘banana’, ‘apple’]
c) (‘apple’, ‘banana’, ‘cherry’)
d) Error: ‘tuple’ object has no attribute ‘sorted’
Answer: b
Explanation: The sorted()
function sorts the elements of the tuple in lexicographic order.
Congratulations on completing the Python Tuples Manipulation MCQS! Tuples are immutable sequences in Python, providing a reliable way to store and access collections of items. By mastering tuple manipulation techniques, you gain the ability to work with immutable data structures efficiently and effectively. Keep practicing and experimenting with Python’s tuple functionalities to become proficient in handling tuples within your programs. If you have any questions or want to delve deeper into any topic, don’t hesitate to continue your learning journey. Happy coding!
You can also enroll in our free Python Course Today!
Read our more articles related to MCQs in Python: