What is a tuple in Python?
a) A collection of unordered elements
b) A collection of ordered elements
c) A single element
d) A data type
Question 2:
How do you create an empty tuple in Python?
a) tuple()
b) empty_tuple = ()
c) empty_tuple = tuple()
d) Both b and c
Question 3:
How do you access the first element of a tuple?
a) tuple[0]
b) tuple.first()
c) tuple.first
d) tuple.get(0)
Question 4:
Which of the following statements is used to add an element to a tuple?
a) tuple.insert(0, element)
b) Tuples are immutable, so elements cannot be added once a tuple is created
c) tuple.add(element)
d) tuple.extend(element)
Question 5:
What is the key difference between a tuple and a list in Python?
a) Tuples are mutable, while lists are immutable
b) Tuples are ordered, while lists are unordered
c) Tuples are immutable, while lists are mutable
d) Tuples can contain only numeric elements
Question 6:
How do you check if an element is present in a tuple?
a) element in tuple
b) tuple.contains(element)
c) tuple.exists(element)
d) element.exists(tuple)
Question 7:
What does the tuple.count(element) method do?
a) Counts the total number of elements in the tuple
b) Counts the occurrences of a specific element in the tuple
c) Counts the sum of all elements in the tuple
d) Counts the average value of elements in the tuple
Question 8:
How do you concatenate two tuples in Python?
a) tuple1 + tuple2
b) tuple1.concat(tuple2)
c) concat(tuple1, tuple2)
d) combine(tuple1, tuple2)
Question 9:
How do you create a tuple with a single element?
a) single_tuple = (1)
b) single_tuple = 1,
c) single_tuple = (1,)
d) Both a and b
Question 10:
Which method is used to find the index of the first occurrence of a specified element in a tuple?
a) tuple.index(element)
b) tuple.find(element)
c) tuple.search(element)
d) tuple.loc(element)
Question 11:
What happens when you try to modify an element in a tuple?
a) It is not possible to modify elements in a tuple as they are immutable
b) The element is updated successfully
c) Python raises an exception
d) The element is deleted from the tuple
Question 12:
How do you create a tuple with elements from 1 to 5 in Python?
a) tuple = (1, 2, 3, 4, 5)
b) tuple = range(1, 6)
c) tuple = tuple(1, 6)
d) tuple = (range(1, 6))
Question 13:
What is the purpose of the len() function when used with a tuple?
a) It returns the total number of elements in the tuple
b) It returns the last element of the tuple
c) It returns the length of each element in the tuple
d) It returns the sum of all elements in the tuple
Question 14:
How do you check if two tuples are equal?
a) tuple1.is_equal(tuple2)
b) tuple1 == tuple2
c) tuple1.equals(tuple2)
d) tuple1.equals(tuple2, strict=True)
Question 15:
What is the purpose of the max() function when used with a tuple?
a) It returns the maximum element in the tuple
b) It returns the index of the maximum element in the tuple
c) It returns the sum of all elements in the tuple
d) It returns the average value of elements in the tuple
Question 16:
Which method is used to remove the last element from a tuple?
a) tuple.remove_last()
b) tuple.pop()
c) tuple.delete_last()
d) Tuples are immutable, so elements cannot be removed
Question 17:
How do you convert a list to a tuple in Python?
a) tuple(list)
b) tuple = list
c) tuple.from_list(list)
d) tuple.convert(list)
Question 18:
What is the purpose of the sorted() function when applied to a tuple?
a) Reverses the order of elements in the tuple
b) Sorts the elements of the tuple in ascending order
c) Removes duplicate elements from the tuple
d) Shuffles the elements of the tuple randomly
Question 19:
What does the tuple.index(element) method return if the element is not found in the tuple?
a) None
b) -1
c) 0
d) Raises a ValueError
Question 20:
What is the output of the following code?
my_tuple = (3, 1, 4, 1, 5, 9, 2)
my_tuple.sort()
print(my_tuple)
a) (1, 1, 2, 3, 4, 5, 9)
b) (9, 5, 4, 3, 2, 1, 1)
c) (1, 1, 2, 3, 4, 5, 9, 2)
d) (1, 2, 3, 4, 5, 9)
Answer:
- b) A collection of ordered elements
- d) Both b and c
- a) tuple[0]
- b) Tuples are immutable, so elements cannot be added once a tuple is created
- c) Tuples are immutable, while lists are mutable
- a) element in tuple
- b) Counts the occurrences of a specific element in the tuple
- a) tuple1 + tuple2
- c) single_tuple = (1,)
- a) tuple.index(element)
- a) It is not possible to modify elements in a tuple as they are immutable
- a) tuple = (1, 2, 3, 4, 5)
- a) It returns the total number of elements in the tuple
- b) tuple1 == tuple2
- a) It returns the maximum element in the tuple
- d) Tuples are immutable, so elements cannot be removed
- a) tuple(list)
- b) Sorts the elements of the tuple in ascending order
- d) Raises a ValueError
- d) (1, 2, 3, 4, 5, 9)
0 Comments:
Post a Comment