Exercise: Python

Questions for: Tuples

Which of the following methods is used to 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 with a negative start parameter can be used to find the last occurrence of an element.
What is the result of the expression (1, 2, 3) + (4,)?
A:
(1, 2, 3, 4)
B:
((1, 2, 3), (4,))
C:
[1, 2, 3, 4]
D:
(1, 2, 3, (4,))
Answer: A
The '+' operator concatenates the two tuples.
What is the purpose of the tuple() constructor?
A:
Creates an empty tuple.
B:
Converts a list to a tuple.
C:
Converts a string to a tuple.
D:
All of the above.
Answer: D
The tuple() constructor can be used for various purposes, including creating an empty tuple and converting other data types to tuples.
How do you unpack the elements of a tuple into separate variables?
A:
var1, var2, var3 = tuple
B:
var1 = tuple[0], var2 = tuple[1], var3 = tuple[2]
C:
var1 = unpack(tuple)
D:
var1, var2, var3 = unpack(tuple)
Answer: A
Using the unpacking syntax, you can assign the elements of a tuple to separate variables.
What is the output of the expression min(('apple', 'orange', 'banana'))?
A:
'apple'
B:
'banana'
C:
'orange'
D:
Raises a TypeError
Answer: A
The min() function returns the smallest element in the tuple based on lexicographic order.
Ad Slot (Above Pagination)
Quiz