Python Questions and Answers – Tuples-3

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Tuples – 3”.

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
View Answer

Answer: c
Explanation: A tuple needn’t be enclosed in parenthesis.
advertisement
advertisement

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',),),)
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

c) ((‘check’,)’check’,)
d)

(('check',)’check’,)
((('check',)’check’,)’check’,)
View Answer
Answer: b
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’)).
 
 

advertisement

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) No, too many values to unpack
d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)
View Answer

Answer: c
Explanation: 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.
advertisement

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
View Answer

Answer: b
Explanation: In the above piece of code, the values of the tuples aren’t being changed. Both the tuples are simply concatenated.

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
View Answer

Answer: c
Explanation: The above piece of code illustrates the unpacking of variables.

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
View Answer

Answer: a
Explanation: The above piece of code illustrates the concept of named tuples.

7. Tuples can’t be made keys of a dictionary.
a) True
b) False
View Answer

Answer: b
Explanation: Tuples can be made keys of a dictionary because they are hashable.

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
View Answer

Answer: d
Explanation: A tuple needn’t be enclosed in parenthesis.

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) None
d) Error, tuple has no attribute sort
View Answer

Answer: d
Explanation: A tuple is immutable thus it doesn’t have a sort attribute.

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
View Answer

Answer: c
Explanation: Tuple doesn’t have any update() attribute because it is immutable.

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
View Answer

Answer: a
Explanation: A list of tuples is a list itself. Hence items of a list can be sorted.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.