Test Series - python

Test Number 48/108

Q: What is the data type of (1)?
A. Tuple
B. Integer
C. List
D. Both tuple and integer
Solution: A tuple of one element must be created as (1,).
Q: 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)
Solution: Tuple slicing exists and a[1:-1] returns (2,3).
Q: What will be the output of the following Python code?

>>> a=(1,2,(4,5))
>>> b=(1,2,(3,4))
>>> a
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
Solution: 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: 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
Solution: Here (“Check”) is a string not a tuple because there is no comma after the element.
Q: 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
Solution: ‘tuple’ object doesn’t support item deletion.
Q: 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
Solution: In the above case, 3 is the starting value to which the sum of the tuple is added to.
Q: 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
Solution: The command del a deletes the entire tuple.
Q: 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
Solution: The variable a has tuples enclosed in a list making it a list of tuples.
Q: 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)
Solution: The method illustrated in the above piece of code is that of naming of slices.

You Have Score    /9