Python Multiple Choice Questions & Answers (MCQs) focuses on “Tuples – 3”.
Q 1. Is the following Python code valid?
>>> a,b,c=1,2,3
>>> a,b,c
A. Yes, [1,2,3] is printed
B. No, invalid syntax
C. Yes, (1,2,3) is printed
D. 1 is printed
Show Answer
Answer:-C. Yes, (1,2,3) is printedExplanation
A tuple needn’t be enclosed in parenthesis.
Q 2. What will be the output of the following Python code?
a = (‘check’,)
n = 2
for i in range(int(n)):
a = (a,)
print(a)
A. Error, tuples are immutable
B. ((‘check’,),)
(((‘check’,),),)
C. ((‘check’,)’check’,)
D. ((‘check’,)’check’,)
(((‘check’,)’check’,)’check’,)
Show Answer
Answer:-B. ((‘check’,),) (((‘check’,),),)Explanation
The loop runs two times and each time the loop runs an extra parenthesis along with a comma is added to the tuple (as a=(a’)).Q 3. Is the following Python code valid?
>>> a,b=1,2,3
A. Yes, this is an example of tuple unpacking. a=1 and b=2
B. Yes, this is an example of tuple unpacking. a=(1,2) and b=3
C. Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
D. No, too many values to unpack
Show Answer
Answer:-D. No, too many values to unpackExplanation
For unpacking to happen, the number of values of the right hand side must be equal to the number of variables on the left hand side.Q 4. What will be the output of the following Python code?
>>> a=(1,2)
>>> b=(3,4)
>>> c=a+b
>>> c
A. (4,6)
B. (1,2,3,4)
C. Error as tuples are immutable
D. None
Show Answer
Answer:-B. (1,2,3,4)Explanation
In the above piece of code, the values of the tuples aren’t being changed. Both the tuples are simply concatenated.
Q 5. What will be the output of the following Python code?
>>> a,b=6,7
>>> a,b=b,a
>>> a,b
A. (6,7)
B. Invalid syntax
C. (7,6)
D. Nothing is printed
Show Answer
Answer:-C. (7,6)Explanation
The above piece of code illustrates the unpacking of variables.
Q 6. What will be the output of the following Python code?
>>> import collections
>>> a=collections.namedtuple(‘a’,[‘i’,’j’])
>>> obj=a(i=4,j=7)
>>> obj
A. a(i=4, j=7)
B. obj(i=4, j=7)
C. (4,7)
D. An exception is thrown
Show Answer
Answer:-A. a(i=4, j=7)Explanation
The above piece of code illustrates the concept of named tuples.
Q 7. Tuples can’t be made keys of a dictionary.
A. True
B. False
Show Answer
Answer:-B. FalseExplanation
Tuples can be made keys of a dictionary because they are hashable.
Q 8. Is the following Python code valid?
>>> a=2,3,4,5
>>> a
A. Yes, 2 is printed
B. Yes, [2,3,4,5] is printed
C. No, too many values to unpack
D. Yes, (2,3,4,5) is printed
Show Answer
Answer:-D. Yes, (2,3,4,5) is printedExplanation
A tuple needn’t be enclosed in parenthesis.
Q 9. What will be the output of the following Python code?
>>> a=(2,3,1,5)
>>> a.sort()
>>> a
A. (1,2,3,5)
B. (2,3,1,5)
C. Error, tuple has no attribute sort
D. None
Show Answer
Answer:-C. Error, tuple has no attribute sortExplanation
A tuple is immutable thus it doesn’t have a sort attribute.
Q 10. Is the following Python code valid?
>>> a=(1,2,3)
>>> b=a.update(4,)
A. Yes, a=(1,2,3,4) and b=(1,2,3,4)
B. Yes, a=(1,2,3) and b=(1,2,3,4)
C. No because tuples are immutable
D. No because wrong syntax for update() method
Show Answer
Answer:-C. No because tuples are immutableExplanation
Tuple doesn’t have any update() attribute because it is immutable.
Q 11. What will be the output of the following Python code?
>>> a=[(2,4),(1,2),(3,9)]
>>> a.sort()
>>> a
A. [(1, 2), (2, 4), (3, 9)]
B. [(2,4),(1,2),(3,9)]
C. Error because tuples are immutable
D. Error, tuple has no sort attribute
Leave a Reply