Exercise: Python

Questions for: Tuples

Which of the following statements about tuples in Python is correct?
A:
Tuples allow in-place modification of elements.
B:
Tuples can be nested to any level.
C:
Tuples can only contain elements of the same data type.
D:
Tuples support random access of elements by index.
Answer: B
Tuples in Python can contain other tuples, allowing nesting to any level.
What is the purpose of the tuple.count() method?
A:
Counts the number of elements in the tuple.
B:
Counts the occurrences of a specific element in the tuple.
C:
Returns the index of the first occurrence of a specific element.
D:
Checks if a specific element exists in the tuple.
Answer: B
The count() method in Python tuples is used to count the occurrences of a specific element.
What happens when you try to modify a tuple?
A:
Raises a TypeError
B:
Automatically converts it to a list.
C:
Adds the new element to the end of the tuple.
D:
Modifies the element at the specified index.
Answer: A
Tuples are immutable in Python, and attempting to modify them raises a TypeError.
Which of the following statements about tuples is true?
A:
Tuples can be resized after creation.
B:
Tuples do not allow duplicate elements.
C:
Elements of a tuple cannot be accessed by index.
D:
Tuples can contain a mix of data types.
Answer: D
Tuples in Python can contain elements of different data types.
How do you create an empty tuple?
A:
empty_tuple = ()
B:
empty_tuple = [ ]
C:
empty_tuple = new Tuple()
D:
empty_tuple = {}
Answer: A
An empty tuple can be created using parentheses with no elements.
Ad Slot (Above Pagination)
Quiz