Python Questions and Answers – Tuples – 2

Python Multiple Choice Questions & Answers (MCQs) focuses on “Tuples – 2”.

Q 1. What is the data type of (1)?
A. Tuple
B.  Integer
C. List
D. Both tuple and integer

Show Answer Answer:-B.  Integer
Explanation A tuple of one element must be created as (1,).

Q 2. If a=(1,2,3,4), a[1:-1] is _________
A. [2,3]
B.  (2,3)
C. (2,3,4)
D.  Error, tuple slicing doesn’t exist

Show Answer Answer:-B.  (2,3)
Explanation Tuple slicing exists and a[1:-1] returns (2,3).


Q 3. What will be the output of the following Python code?

>>> a=(1,2,(4,5))
>>> b=(1,2,(3,4))
>>> a<b
A. False
B.  True
C. Error, < operator is not valid for tuples
D. Error, < operator is valid for tuples but not if there are sub-tuples

Show Answer Answer:-A. False
Explanation Since the first element in the sub-tuple of a is larger that the first element in the subtuple of b, False is printed.

Q 4. What will be the output of the following Python code?

>>> a=(“Check”)*3
>>> a
A. (‘Check’,’Check’,’Check’)
B. * Operator not valid for tuples
C.  (‘CheckCheckCheck’)
D.  Syntax error

Show Answer Answer:-C.  (‘CheckCheckCheck’)
Explanation Here (“Check”) is a string not a tuple because there is no comma after the element.


Q 5. What will be the output of the following Python code?

>>> a=(1,2,3,4)
>>> del(a[2])
A. Now, a=(1,2,4)
B. Now, a=(1,3,4)
C. Now a=(3,4)
D. Error as tuple is immutable

Show Answer Answer:-D. Error as tuple is immutable
Explanation ‘tuple’ object doesn’t support item deletion.


Q 6. What will be the output of the following Python code?

>>> a=(2,3,4)
>>> sum(a,3)
A. 9
B. 12
C. The method sum() doesn’t exist for tuples
D. Too many arguments for sum() method

Show Answer Answer:-B. 12
Explanation In the above case, 3 is the starting value to which the sum of the tuple is added to.


Q 7. Is the following Python code valid?

>>> a=(1,2,3,4)
>>> del a
A. No because tuple is immutable
B. Yes, first element in the tuple is deleted
C.  Yes, the entire tuple is deleted
D.  No, invalid syntax for del method

Show Answer Answer:-C.  Yes, the entire tuple is deleted
Explanation The command del a deletes the entire tuple.


Q 8. What type of data is: a=[(1,1),(2,4),(3,9)]?
A. Array of tuples
B. List of tuples
C. Tuples of lists
D. Invalid type

Show Answer Answer:-B. List of tuples
Explanation The variable a has tuples enclosed in a list making it a list of tuples.


Q 9. What will be the output of the following Python code?

>>> a=(0,1,2,3,4)
>>> b=slice(0,2)
>>> a[b]
A. (0,2)
B. [0,2]
C. (0,1)
D. Invalid syntax for slicing

Show Answer Answer:-C. (0,1)
Explanation The method illustrated in the above piece of code is that of naming of slices.


Q 10. Is the following Python code valid?

>>> a=(1,2,3)
>>> b=(‘A’,’B’,’C’)
>>> c=tuple(zip(a,b))
A.  Yes, c will be ((1, ‘A’), (2, ‘B’), (3, ‘C’))
B.  Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
C. No because tuples are immutable
D.  No because the syntax for zip function isn’t valid

Show Answer Answer:-A.  Yes, c will be ((1, ‘A’), (2, ‘B’), (3, ‘C’))
Explanation Zip function combines individual elements of two iterables into tuples. Execute in Python shell to verify.


Posted

in

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page