Tuples in Python are fundamental data structure that function as immutable sequences. They are used to store multiple items in a single variable and can contain elements of different types. Here we present 15 multiple-choice questions to test your knowledge of Python Tuples. Each MCQ has the correct answer with an explanation.
1. How is a tuple with a single element defined in Python?
a) (item,)
b) (item)
c) item,
d) [item]
Answer:
a) (item,)
Explanation:
In Python, a single element tuple must have a comma after the item, otherwise, it's not recognized as a tuple.
2. How do you access the second element of a tuple 'my_tuple'?
a) my_tuple[1]
b) my_tuple[2]
c) my_tuple.get(1)
d) my_tuple(1)
Answer:
a) my_tuple[1]
Explanation:
Tuple indices start at 0. So the second element is accessed using my_tuple[1].
3. Which of the following is true about modifying tuples?
a) Tuples can be resized
b) Elements of a tuple can be changed
c) Tuples can never be modified
d) Only string elements in a tuple can be changed
Answer:
c) Tuples can never be modified
Explanation:
Tuples are immutable in Python, meaning their size and the values of elements cannot be changed.
4. How do you unpack the values of a tuple 't' into variables a, b, and c?
a) a, b, c = t
b) a, b, c = unpack(t)
c) t.unpack(a, b, c)
d) a, b, c = t[]
Answer:
a) a, b, c = t
Explanation:
The syntax a, b, c = t automatically unpacks the values of tuple 't' into variables a, b, and c.
5. How do you loop through a tuple named 'my_tuple' to print each value?
a) for i in my_tuple: print(i)
b) for i in range(len(my_tuple)): print(my_tuple[i])
c) Both a and b
d) None of the above
Answer:
c) Both a and b
Explanation:
Both methods are valid. You can loop directly through the tuple items or loop through the tuple indices.
6. How do you concatenate tuple1 and tuple2 in Python?
a) tuple1 + tuple2
b) tuple1 & tuple2
c) tuple1.concat(tuple2)
d) concat(tuple1, tuple2)
Answer:
a) tuple1 + tuple2
Explanation:
The + operator is used to concatenate tuples in Python.
7. Which method would you use to count the occurrences of an element in a tuple?
a) count()
b) index()
c) find()
d) lookup()
Answer:
a) count()
Explanation:
The count() method returns the number of times an element appears in a tuple.
8. What is the output of ('Hi!') * 4 in Python?
a) ('Hi!', 'Hi!', 'Hi!', 'Hi!')
b) 'Hi!Hi!Hi!Hi!'
c) Error
d) ('Hi!' * 4)
Answer:
b) 'Hi!Hi!Hi!Hi!'
Explanation:
Multiplying a string by an integer n concatenates the string n times.
9. How do you convert a list [1, 2, 3] to a tuple in Python?
a) tuple([1, 2, 3])
b) ([1, 2, 3]).toTuple()
c) convert([1, 2, 3], tuple)
d) Tuple([1, 2, 3])
Answer:
a) tuple([1, 2, 3])
Explanation:
The tuple() function is used to convert a list to a tuple.
10. What is the result of ('a', 'b') < ('c', 'd') in Python?
a) True
b) False
c) Error
d) None
Answer:
a) True
Explanation:
Tuple comparison is performed element by element. 'a' is less than 'c', so the expression is True.
11. What does my_tuple.index('a') return if my_tuple = ('a', 'b', 'a')?
a) 0
b) 1
c) 2
d) 3
Answer:
a) 0
Explanation:
The index() method returns the index of the first occurrence of the value.
12. What does len(('a', 'b', 'c')) return?
a) 3
b) 2
c) 1
d) Error
Answer:
a) 3
Explanation:
len() returns the number of elements in the tuple.
13. How do you create a nested tuple?
a) ((1, 2), (3, 4))
b) [1, 2], [3, 4]
c) (1, 2) and (3, 4)
d) Nested(1, 2, 3, 4)
Answer:
a) ((1, 2), (3, 4))
Explanation:
Nested tuples are created by placing tuples inside another tuple.
14. What will ('Hello',) * 2 produce?
a) ('Hello', 'Hello')
b) ('HelloHello',)
c) ('Hello') * 2
d) Error
Answer:
a) ('Hello', 'Hello')
Explanation:
Multiplying a tuple by an integer n repeats the tuple n times.
15. What is the output of the following code?
t = (1, 2, [3, 4])
t[2][0] = 'x'
print(t)
a) (1, 2, ['x', 4])
b) Error
c) (1, 'x', [3, 4])
d) (1, 2, [3, 'x'])
Answer:
a) (1, 2, ['x', 4])
Explanation:
While tuples are immutable, their mutable elements (like lists) can be modified.
Comments
Post a Comment