Python MCQs on Tuple: SET 3

 

21. 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

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

 

22. 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’,)

Check Answer

Answer: b
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’)).

 

23. 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)

Check Answer

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

 

24. 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

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

 

25. 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

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

 

26. 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

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

 

27. Tuples can’t be made keys of a dictionary.

a) True

b) False

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

 

28. 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

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

 

29. 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

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

 

30. 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

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

Page  1  2  3

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment