Exercise: Python

Questions for: Tuples

Which of the following methods is used to find the index of the first occurrence of a specific element in a tuple?
A:
tuple.find(element)
B:
tuple.search(element)
C:
tuple.index(element)
D:
tuple.locate(element)
Answer: C
The index() method is used to find the index of the first occurrence of a specific element in a tuple.
What is the result of the expression tuple(('a', 'b', 'c'))[::-1]?
A:
('a', 'b', 'c')
B:
('c', 'b', 'a')
C:
['c', 'b', 'a']
D:
('c', 'b', 'a')
Answer: B
The [::-1] slice reverses the order of elements in the tuple.
How do you find the length of a tuple?
A:
tuple.length()
B:
tuple.size()
C:
len(tuple)
D:
length(tuple)
Answer: C
The len() function is used to find the length of a tuple.
What is the result of the expression tuple('hello')?
A:
('h', 'e', 'l', 'l', 'o')
B:
(104, 101, 108, 108, 111)
C:
('hello')
D:
['h', 'e', 'l', 'l', 'o']
Answer: A
The tuple() constructor can convert a string to a tuple of its characters.
What is the output of the expression max(('apple', 'orange', 'banana'))?
A:
'apple'
B:
'banana'
C:
'orange'
D:
Raises a TypeError
Answer: C
The max() function returns the largest element in the tuple based on lexicographic order.
Ad Slot (Above Pagination)
Quiz