30+ MCQs on Python Tuple Manipulation

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!

Python Tuple Manipulation

30+ MCQs on Python Tuple Manipulation

Q1. What is the output of the following Python code snippet?

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.

Q2. Which of the following operations is not valid for tuples in Python?

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.

Q3. What will be the output of the following Python code snippet?

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).

Q4. Which of the following methods is used to find the index of a specified element in a tuple in Python?

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.

Q5. What will be the output of the following Python code snippet?

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.

Q6. Which of the following methods is used to count the number of occurrences of a specified element in a tuple in Python?

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.

Q7. What will be the output of the following Python code snippet?

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.

Q8. Which of the following methods is used to reverse the elements of a tuple in Python?

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.

Q9. What will be the output of the following Python code snippet?

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.

Q10. Which of the following methods is used to concatenate two tuples in Python?

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.

Q11. What will be the output of the following Python code snippet?

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.

Q12. Which of the following methods is used to find the length of a tuple in Python?

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.

Q13. What will be the output of the following Python code snippet?

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.

Q14. Which of the following methods is used to convert a tuple into a list in Python?

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.

Q15. What will be the output of the following Python code snippet?

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.

Q16. What is the output of the following Python code snippet?

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.

Q17. Which of the following statements about tuples in Python is true?

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 [].

Q18. What will be the output of the following Python code snippet?

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.

Q19. Which of the following methods is used to remove an element from a tuple in Python?

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.

Q20. What will be the output of the following Python code snippet?

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).

Q21. What does the 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.

Q22. What will be the output of the following Python code snippet?

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.

Q23. What will be the output of the following Python code snippet?

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.

Q24. Which of the following statements about tuples in Python is false?

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.

Q25. What will be the output of the following Python code snippet?

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.

Q26. What will be the output of the following Python code snippet?

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.

Q27. Which of the following methods can be used to convert a tuple into a dictionary in Python?

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.

Q28. What will be the output of the following Python code snippet?

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.

Q29. What will be the output of the following Python code snippet?

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.

Q30. Which of the following methods can be used to check if an element exists in a tuple in Python?

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.

Q31. What will be the output of the following Python code snippet?

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.

Q32. What will be the output of the following Python code snippet?

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.

Q33. What will be the output of the following Python code snippet?

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 out free Python Course Today!

Read our more articles related to MCQs in Python:

Source link

Picture of quantumailabs.net
quantumailabs.net

Leave a Reply

Your email address will not be published. Required fields are marked *