Python Questions and Answers – Tuples – 2

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

1. What is the data type of (1)?
a) Tuple
b) Integer
c) List
d) Both tuple and integer
View Answer

Answer: b
Explanation: A tuple of one element must be created as (1,).

2. If a=(1,2,3,4), a[1:-1] is _________
a) Error, tuple slicing doesn’t exist
b) [2,3]
c) (2,3,4)
d) (2,3)
View Answer

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

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

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

Answer: a
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.
Note: Join free Sanfoundry classes at Telegram or Youtube

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

Answer: c
Explanation: Here (“Check”) is a string not a tuple because there is no comma after the element.
advertisement

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

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

Answer: d
Explanation: ‘tuple’ object doesn’t support item deletion.

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

>>> a=(2,3,4)
>>> sum(a,3)

a) Too many arguments for sum() method
b) The method sum() doesn’t exist for tuples
c) 12
d) 9
View Answer

Answer: c
Explanation: In the above case, 3 is the starting value to which the sum of the tuple is added to.

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

Answer: c
Explanation: The command del a deletes the entire tuple.

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

Answer: b
Explanation: The variable a has tuples enclosed in a list making it a list of tuples.

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

>>> a=(0,1,2,3,4)
>>> b=slice(0,2)
>>> a[b]

a) Invalid syntax for slicing
b) [0,2]
c) (0,1)
d) (0,2)
View Answer

Answer: c
Explanation: The method illustrated in the above piece of code is that of naming of slices.

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

Answer: a
Explanation: Zip function combines individual elements of two iterables into tuples. Execute in Python shell to verify.

Sanfoundry Global Education & Learning Series – Python.

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

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.