Exercise: Python

Questions for: Tuples

What is the result of the expression (1, 2, 3) * 2?
A:
(1, 2, 3, 1, 2, 3)
B:
((1, 2, 3), (1, 2, 3))
C:
[1, 2, 3, 1, 2, 3]
D:
(2, 4, 6)
Answer: A
The '*' operator repeats the elements of the tuple.
How can you find the index of the last occurrence of a specific element in a tuple?
A:
tuple.last_index(element)
B:
tuple.index_last(element)
C:
tuple.index(element, -1)
D:
tuple.find_last(element)
Answer: C
The index() method can be used with a negative start parameter to find the last occurrence of an element.
What happens if you try to concatenate two tuples using the '+' operator?
A:
Raises a TypeError
B:
Merges the tuples into a single tuple.
C:
Creates a list containing both tuples.
D:
Concatenates the tuples into a new tuple.
Answer: D
The '+' operator concatenates tuples, creating a new tuple with elements from both tuples.
How can you create a tuple with a single element?
A:
single_tuple = (1)
B:
single_tuple = (1,)
C:
single_tuple = 1
D:
single_tuple = tuple(1)
Answer: B
Using a comma after the single element ensures it is treated as a tuple.
What is the purpose of the tuple.index() method?
A:
Returns the total number of elements in the tuple.
B:
Finds the index of the last occurrence of a specific element.
C:
Finds the index of the first occurrence of a specific element.
D:
Adds a new element to the end of the tuple.
Answer: C
The index() method is used to find the index of the first occurrence of a specific element in a tuple.
Ad Slot (Above Pagination)
Quiz