Python Tuple MCQ : Set 2

Python Tuple MCQ

1). Which of the following methods is used to insert an element at a specific index in a tuple?

a) insert()
b) add()
c) append()
d) extend()

Correct answer is: a
Explanation: There is no built-in method to insert an element at a specific index in a tuple because tuples are immutable.

2). Can a tuple be converted into a string using the `str()` function?

a) Yes
b) No

Correct answer is: a
Explanation: The `str()` function can be used to convert a tuple into a string representation.

3). Which method is used to remove a specific element from a tuple?

a) remove()
b) delete()
c) discard()
d) Tuples are immutable and cannot be modified)

Correct answer is: d
Explanation: Tuples are immutable, so elements cannot be removed directly.

4). Which of the following statements about tuples is false?

a) Tuples are enclosed in parentheses.
b) Tuples can be used as keys in dictionaries.
c) Tuples are immutable.
d) Tuples can contain elements of different data types.

Correct answer is: d
Explanation: Tuples can contain elements of different data types, so option (d) is false.

5). Which of the following methods is used to convert a list into a tuple?

a) to_tuple()
b) convert_tuple()
c) tuple()
d) list_to_tuple()

Correct answer is: c
Explanation: The `tuple()` function is used to convert a list into a tuple.

6). Which of the following methods is used to find the index of the last occurrence of a specific element in a tuple?

a) count()
b) find()
c) index()
d) rindex()

Correct answer is: d
Explanation: The `rindex()` method is used to find the index of the last occurrence of a specific element in a tuple.

7). Which of the following statements is true regarding tuple unpacking?

a) Tuple unpacking allows assigning multiple variables from a single tuple.
b) Tuple unpacking can only be done with tuples of the same length.
c) Tuple unpacking is not supported in Python.
d) Tuple unpacking is used to merge two tuples into one.

Correct answer is: a
Explanation: Tuple unpacking allows assigning multiple variables from a single tuple, making option (a) true.

8).  Can a tuple be modified after it is created?

a) Yes
b) No

Correct answer is: b
Explanation: Tuples are immutable, meaning they cannot be modified after creation.

9). Which of the following methods is used to check if all elements in a tuple are of the same value?

a) same()
b) equal()
c) all_equal()
d) None of the above

Correct answer is: a
Explanation: The method same() does not exist in Python. To check if all elements in a tuple are of the same value, we can compare the first element with the rest of the elements using the all() function.

10). Which of the following methods is used to find the minimum value in a tuple?

a) min()
b) minimum()
c) smallest()
d) None of the above

Correct answer is: a
Explanation: The min() function is used to find the minimum value in a tuple. It returns the smallest element based on their natural ordering.

11). Can a tuple be used as a key in a set?

a) Yes
b) No

Correct answer is: b
Explanation: No, a tuple cannot be used as a key in a set. Sets require elements to be hashable, and since tuples are mutable, they are not hashable.

12).  Which method is used to find the maximum value in a tuple?

a) max()
b) maximum()
c) largest()
d) None of the above

Correct answer is: a
Explanation: The max() function is used to find the maximum value in a tuple. It returns the largest element based on their natural ordering.

13). Can a tuple contain mutable objects like lists?

a) Yes
b) No

Correct answer is: a
Explanation: Yes, a tuple can contain mutable objects like lists. However, the tuple itself remains immutable, but the objects it contains can be mutable.

14). Which of the following methods is used to find the length of a tuple?

a) length()
b) count()
c) size()
d) len()

Correct answer is: d
Explanation: The built-in len() function is used to find the length of a tuple. It returns the number of elements in the given tuple.

15). Can a tuple be used as an element in another tuple?

a) Yes
b) No

Correct answer is: a
Explanation: Yes, a tuple can be used as an element in another tuple. Tuples can contain elements of any data type, including other tuples.

16). Which method is used to convert a tuple into a set?

a) set()
b) convert_set()
c) to_set()
d) None of the above

Correct answer is: a
Explanation: The set() function can be used to convert a tuple into a set. It creates a new set containing the unique elements from the tuple.

17). Which of the following statements is true regarding the memory efficiency of tuples compared to lists?

a) Tuples are more memory-efficient than lists.
b) Lists are more memory-efficient than tuples.
c) Tuples and lists have the same memory efficiency.
d) Memory efficiency does not depend on the data structure.

Correct answer is: c
Explanation: Tuples and lists have the same memory efficiency in terms of storing elements. The memory consumption depends on the number and size of the elements, not the data structure itself.

18). Can a tuple be sorted using the `sort()` method?

a) Yes
b) No

Correct answer is: b
Explanation: No, tuples are immutable, and the sort() method is only available for mutable sequences like lists. To sort a tuple, you can convert it to a list, sort the list, and then convert it back to a tuple.

19). What is the output of the following code?

tup = (41, 15, 69, 55)
print(“Maximum value: “, max(tup))

a). Maximum value: 41
b). Maximum value: 69
c). Maximum value: 55
d). Maximum value: 15

Corresct answer is: b) Maximum value: 69
Explanation: The max() function returns the maximum value from the given sequence. In this case, the sequence is the tuple tup containing the values (41, 15, 69, 55). The maximum value in the tuple is 69, so the output will be “Maximum value: 69”.

20). What is the output of the following code?

tup = (36, 5, 79, 25)
print(“Minimum value: “, min(tup))

a). Minimum value: 36
b). Minimum value: 5
c). Minimum value: 79
d). Minimum value: 25

Corresct answer is: b) Minimum value: 5
Explanation: The min() function returns the minimum value from a sequence. In this case, the sequence is the tuple tup containing the values (36, 5, 79, 25). The smallest value in the tuple is 5, so the output will be “Minimum value: 5”.

21). What is the output of the following code?

list1 = [4, 6, 8]
list2 = [7, 1, 4]
tup = tuple(zip(list1, list2))
print(tup)

a). [(4, 7), (6, 1), (8, 4)]
b). [(7, 4), (1, 6), (4, 8)]
c). [(4, 6, 8), (7, 1, 4)]
d). [(4, 7), (6, 1), (8, 4), (0, 0)]

Corresct answer is: a). [(4, 7), (6, 1), (8, 4)]
Explanation: The zip() function takes two or more iterables and aggregates their elements into tuples. In this case, it combines the elements of list1 and list2 into tuples. The output will be [(4, 7), (6, 1), (8, 4)].

22). What is the output of the following code?

list1 = [4, 6, 3, 8]
tup = [(val, pow(val, 3)) for val in list1]
print(tup)

a). `[(4, 64), (6, 216), (3, 27), (8, 512)]`
b). `[(4, 12), (6, 18), (3, 9), (8, 24)]`
c). `[(64, 4), (216, 6), (27, 3), (512, 8)]`
d). `[(12, 4), (18, 6), (9, 3), (24, 8)]`

Corresct answer is: a). `[(4, 64), (6, 216), (3, 27), (8, 512)]`

Explanation: The code snippet demonstrates list comprehension and uses the `pow()` function to calculate the cube of each value in the `list1` list. The resulting list comprehension creates a new list of tuples, where each tuple consists of the original value from `list1` and its cube. The output will be `[(4, 64), (6, 216), (3, 27), (8, 512)]`.

23). What is the output of the following code?

tup = (4, 8, 9, 1)
print(“Number in the tuple with index 2: “, tup[2])

a). Number in the tuple with index 2: 4
b). Number in the tuple with index 2: 8
c). Number in the tuple with index 2: 9
d). Number in the tuple with index 2: 1

Corresct answer is: c). Number in the tuple with index 2: 9
Explanation: The code defines a tuple `tup` with elements (4, 8, 9, 1). The index starts from 0, so `tup[2]` accesses the element at index 2, which is 9. Therefore, the output will be “Number in the tuple with index 2: 9”.

24). What is the output of the following code?

tup = (6, 7, 3)
(a, b, c) = tup
print(“a: “, a)
print(“b: “, b)
print(“c: “, c)

a). a: 6, b: 7, c: 3
b). a: 7, b: 3, c: 6
c). a: 6, b: 3, c: 7
d). a: 7, b: 6, c: 3

Corresct answer is: a). a: 6, b: 7, c: 3
Explanation: In the given code, the tuple tup contains three elements: 6, 7, and 3. The variables a, b, and c are assigned the values of the corresponding elements in the tuple using tuple unpacking. Therefore, a will be 6, b will be 7, and c will be 3.

25). What is the output of the following code?

tup = (18, 65, 3, 45)
print(“Old tuple: “, tup)
tup = list(tup)
tup.append(15)
tup = tuple(tup)
print(“New tuple: “, tup)

a). Old tuple: (18, 65, 3, 45)
New tuple: (18, 65, 3, 45, 15)
b). Old tuple: (18, 65, 3, 45)
New tuple: (18, 65, 3, 45)
c). Old tuple: [18, 65, 3, 45]
New tuple: (18, 65, 3, 45, 15)
d). Old tuple: [18, 65, 3, 45]
New tuple: [18, 65, 3, 45, 15]

Corresct answer is: c). Old tuple: [18, 65, 3, 45]
New tuple: (18, 65, 3, 45, 15)
Explanation: The code first converts the tuple tup to a list using the list() function. Then, the number 15 is appended to the list. Finally, the list is converted back to a tuple using the tuple() function. Therefore, the output will be:
Old tuple: [18, 65, 3, 45]
New tuple: (18, 65, 3, 45, 15)

Leave a Comment