Python Programming - Tuples

Exercise : Tuples - General Questions
  • Tuples - General Questions
26.
How can you create a shallow copy of a tuple?
new_tuple = old_tuple.copy()
new_tuple = old_tuple.clone()
new_tuple = copy(old_tuple)
new_tuple = old_tuple[:]
Answer: Option
Explanation:
The slicing technique [:] creates a shallow copy of the tuple.

27.
Which method is used to remove all occurrences of a specific element from a tuple?
tuple.remove_all(element)
tuple.clear(element)
tuple.removeAll(element)
tuple = tuple.replace(element, ())
Answer: Option
Explanation:
Assigning the result of replace() with an empty tuple removes all occurrences of the specified element.

28.
What is the output of the expression min(('apple', 'orange', 'banana'))?
'apple'
'banana'
'orange'
Raises a TypeError
Answer: Option
Explanation:
The min() function returns the smallest element in the tuple based on lexicographic order.

29.
Which of the following statements about tuples in Python is correct?
Tuples can be modified after creation.
Tuples can only store elements of the same data type.
Tuples have a fixed size and cannot be resized.
Tuples can be used as keys in a dictionary.
Answer: Option
Explanation:
Tuples in Python are immutable, meaning their size and elements cannot be changed after creation.

30.
How do you unpack the elements of a tuple into separate variables?
var1, var2, var3 = tuple
var1 = tuple[0], var2 = tuple[1], var3 = tuple[2]
var1 = unpack(tuple)
var1, var2, var3 = unpack(tuple)
Answer: Option
Explanation:
Using the unpacking syntax, you can assign the elements of a tuple to separate variables.