Exercise: Python

Questions for: Tuples

Which method is used to remove all occurrences of a specific element from a tuple?
A:
tuple.remove_all(element)
B:
tuple.clear(element)
C:
tuple.removeAll(element)
D:
tuple = tuple.replace(element, ())
Answer: D
Assigning the result of replace() with an empty tuple removes all occurrences of the specified element.
How can you create a shallow copy of a tuple?
A:
new_tuple = old_tuple.copy()
B:
new_tuple = old_tuple.clone()
C:
new_tuple = copy(old_tuple)
D:
new_tuple = old_tuple[:]
Answer: D
The slicing technique [:] creates a shallow copy of the tuple.
What is the result of the expression len(('apple', 'orange', 'banana'))?
A:
3
B:
6
C:
16
D:
1
Answer: A
The len() function returns the number of elements in the tuple, and in this case, there are three elements.
Which of the following methods is used to find the total number of occurrences of a specific element in a tuple?
A:
tuple.total(element)
B:
tuple.count(element)
C:
tuple.find(element)
D:
tuple.occurrences(element)
Answer: B
The count() method is used to find the total number of occurrences of a specific element in a tuple.
How do you create a tuple with repeated elements?
A:
new_tuple = (1, 2, 1, 2, 1, 2)
B:
new_tuple = tuple(1, 2, 1, 2, 1, 2)
C:
new_tuple = (1, 2) * 3
D:
new_tuple = tuple.repeat(1, 2)
Answer: A
Repeating elements can be done directly within the tuple.
Ad Slot (Above Pagination)
Quiz