Exercise: Python

Questions for: Tuples

What does the sorted() function return when applied to a tuple?
A:
A sorted list
B:
A sorted tuple
C:
Raises a TypeError
D:
An error, as tuples cannot be sorted
Answer: A
The sorted() function returns a sorted list, even when applied to a tuple.
What is the correct way to access the second element of a tuple?
A:
tuple[2]
B:
tuple.get(1)
C:
tuple[1]
D:
tuple.element(2)
Answer: C
In Python, indexing starts from 0, so the second element can be accessed using index 1.
Which method is used to remove the first occurrence of a specific element in a tuple?
A:
tuple.delete(element)
B:
tuple.remove(element)
C:
tuple.discard(element)
D:
tuple.pop(element)
Answer: B
The remove() method is used to remove the first occurrence of a specific element in a tuple.
How do you create a tuple with the elements from 1 to 5?
A:
new_tuple = (1, 2, 3, 4, 5)
B:
new_tuple = tuple(range(1, 6))
C:
new_tuple = (1 to 5)
D:
new_tuple = [1, 2, 3, 4, 5]
Answer: B
The range() function generates a sequence of numbers, and tuple() converts it to a tuple.
What is the primary difference between tuples and lists?
A:
Tuples are mutable, while lists are immutable.
B:
Tuples are ordered, while lists are unordered.
C:
Tuples allow duplicate elements, while lists do not.
D:
Tuples are immutable, while lists are mutable.
Answer: D
Tuples cannot be modified once created, making them immutable.
Ad Slot (Above Pagination)
Quiz