Python Tuple MCQ
1). What is a tuple in Python?
a) A mutable data type
b) An immutable data type
c) A collection of key-value pairs
d) A dynamic array
Correct answer is: b
Explanation: A tuple is an immutable data type in Python, meaning its elements cannot be modified once defined
2). Which of the following is the correct way to define a tuple in Python?
a) [1, 2, 3]
b) {1, 2, 3}
c) (1, 2, 3)
d) {1: 2, 3: 4}
Correct answer is: c
Explanation: Tuples are defined using parentheses in Python, as shown in option (c)
3). Which of the following operations is not supported on tuples?
a) Accessing elements by index
b) Modifying elements
c) Concatenating two tuples
d) Slicing
Correct answer is: b
Explanation: Tuples are immutable, so modifying elements is not supported) Options (a), (c), and (d) are valid operations on tuples.
4). How can you access the second element of a tuple named “tuple”?
a) tuple[1]
b) tuple(1)
c) tuple[2]
d) tuple(2)
Correct answer is: a
Explanation: In Python, indexing starts from 0, so the second element can be accessed using tuple[1].
5). Which of the following statements is true about tuples?
a) Tuples can contain elements of different data types.
b) Tuples can be resized after creation.
c) Tuples preserve the order of elements.
d) Tuples can be used as keys in dictionaries.
Correct answer is: c
Explanation: Tuples preserve the order of elements, making them suitable for situations where element order is important, such as storing coordinates.
6). What is the output of the following code?
mtuple = (1, 2, 3)
print(uple[0:2])
a) (1, 2)
b) (2, 3)
c) (2, 3)
d) (1, 2, 3)
Correct answer is: a
Explanation: Slicing the tuple from index 0 to 2 (exclusive) will result in (1, 2).
7). Which method can be used to find the number of occurrences of a specific element in a tuple?
a) count()
b) find()
c) index()
d) len()
Correct answer is: a
Explanation: The `count()` method is used to find the number of occurrences of a specific element in a tuple.
8). Can tuples be nested within other tuples in Python?
a) Yes
b) No
Correct answer is: a
Explanation: Tuples can be nested within other tuples in Python, allowing the creation of more complex data structures.
9). What is the output of the following code?
tuple = (1, 2, 3) * 2
print(tuple)
a) (1, 2, 3)
b) (2, 4, 6)
c) (1, 2, 3, 1, 2, 3)
d) (1, 1, 2, 2, 3, 3)
Correct answer is: c
Explanation: The `*` operator duplicates the tuple, resulting in (1, 2, 3, 1, 2, 3).
10). Which of the following is not a valid way to iterate over a tuple in Python?
a) Using a for loop
b) Using the `enumerate()` function
c) Using the `iter()` function
d) Using a while loop
Correct answer is: d
Explanation: A while loop is not a valid way to iterate over a tuple directly. Other options (a), (b), and (c) are valid)
11). Which of the following statements is true regarding the memory consumption of tuples and lists?
a) Tuples consume less memory than lists.
b) Lists consume less memory than tuples.
c) Tuples and lists consume the same amount of memory.
d) Memory consumption depends on the number of elements, not the data type.
Correct answer is: a
Explanation: Tuples consume less memory than lists because they are immutable and have a fixed size.
12). Which method is used to convert a tuple into a list?
a) to_list()
b) convert_list()
c) list()
d) tuple_to_list()
Correct answer is: c
Explanation: The `list()` function is used to convert a tuple into a list.
13). What is the output of the following code?
tuple = (‘SQATOOLS’,)
print(type(tuple))
a) <class ‘tuple’>
b) <class ‘list’>
c) <class ‘str’>
d) <class ‘set’>
Correct answer is: a
Explanation: The comma after ‘SQATOOLS’ indicates that this is a tuple, so the output will be <class ‘tuple’>.
14). Which of the following is the correct way to compare two tuples for equality in Python?
a) tuple1 == tuple2
b) tuple1.equals(tuple2)
c) tuple1.compare(tuple2)
d) tuple1 is tuple2
Correct answer is: a
Explanation: The `==` operator is used to compare two tuples for equality.
15). Can tuples contain mutable elements like lists or dictionaries?
a) Yes
b) No
Correct answer is: a
Explanation: Tuples can contain mutable elements like lists or dictionaries, but the tuple itself remains immutable.
16). Which method is used to sort a tuple in ascending order?
a) sort()
b) sorted()
c) sort_asc()
d) order()
Correct answer is: b
Explanation: The `sorted()` function is used to sort a tuple in ascending order.
17). Which method is used to find the index of the first occurrence of a specific element in a tuple?
a) count()
b) find()
c) index()
d) position()
Correct answer is: c
Explanation: The `index()` method in Python is used to find the index of the first occurrence of a specific element in a tuple.
18). What is the output of the following code?
tuple = (4, 5, 6)
a, b, c = tuple
print(b)
a) 4
b) 5
c) 6
d) Error: too many values to unpack
Correct answer is: b
Explanation: The tuple elements are assigned to variables a, b, and c, respectively. Printing b will give the output 5.
19). Which of the following is a valid way to delete an entire tuple in Python?
a) del my_tuple
b) my_tuple.clear()
c) my_tuple.delete()
d) my_tuple = ()
Correct answer is: a
Explanation: The `del` keyword is used to delete an entire tuple in Python.
20). Which of the following methods is used to reverse the order of elements in a tuple?
a) reverse()
b) reversed()
c) invert()
d) flip()
Correct answer is: b
Explanation: The `reversed()` function is used to reverse the order of elements in a tuple.
21). Can a tuple be used as a key in a dictionary?
a) Yes
b) No
Correct answer is: a
Explanation: Tuples can be used as keys in dictionaries because they are immutable.
22). Which of the following methods is used to check if an element exists in a tuple?
a) exists()
b) contains()
c) find()
d) in
Correct answer is: d
Explanation: The `in` keyword is used to check if an element exists in a tuple.
23). Which of the following statements is true regarding the ordering of elements in a tuple?
a) Tuples are unordered)
b) Tuples maintain the order in which elements were added)
c) The order of elements in a tuple is random.
d) The order of elements in a tuple depends on their values.
Correct answer is: b
Explanation: Tuples maintain the order in which elements were added, preserving the sequence of elements.
24). Which method is used to concatenate two tuples?
a) add()
b) concatenate()
c) extend()
d) +
Correct answer is: d
Explanation: The `+` operator can be used to concatenate two tuples.
25). Can a tuple contain duplicate elements?
a) Yes
b) No
Correct answer is: a
Explanation: Tuples can contain duplicate elements.