Exercise: Python

Questions for: Tuples

What is the purpose of the tuple.__contains__(element) method?
A:
Checks if the tuple contains the specified element.
B:
Adds the specified element to the tuple.
C:
Removes the specified element from the tuple.
D:
Returns the count of occurrences of the specified element.
Answer: A
The __contains__ method is used to check if the tuple contains a specified element.
What is the output of the expression tuple('python')[::-1]?
A:
('n', 'o', 'h', 't', 'y', 'p')
B:
('p', 'y', 't', 'h', 'o', 'n')
C:
['n', 'o', 'h', 't', 'y', 'p']
D:
('n', 'o', 'h', 't', 'y', 'p')
Answer: A
The [::-1] slice reverses the order of elements in the tuple.
What is the purpose of the tuple.extend() method?
A:
Extends the size of the tuple.
B:
Adds elements to the end of the tuple.
C:
Merges two tuples into a new one.
D:
Raises a TypeError
Answer: D
The tuple type in Python does not have an extend() method.
How can you delete an entire tuple?
A:
delete(tuple)
B:
del tuple
C:
tuple.delete()
D:
tuple.clear()
Answer: B
The del keyword is used to delete a tuple.
What is the result of the expression tuple(('cat', 'dog', 'rabbit')) * 2?
A:
('cat', 'dog', 'rabbit', 'cat', 'dog', 'rabbit')
B:
['cat', 'dog', 'rabbit', 'cat', 'dog', 'rabbit']
C:
(('cat', 'dog', 'rabbit'), ('cat', 'dog', 'rabbit'))
D:
('catdograbbit', 'catdograbbit')
Answer: A
The '*' operator is used to repeat the elements of the tuple.
Ad Slot (Above Pagination)
Quiz