Tuples In Python: Tuples MCQs in Python Language

Tuples In Python. MCQs on Tuples in Python Language. 

 

Tuples In Python

 

 

What will be the output of the following 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

 

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

 

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

numberGames = {}
numberGames[(1,2,4)] = 8
numberGames[(4,2,1)] = 10
numberGames[(1,2)] = 12
sum = 0
for k in numberGames:
sum += numberGames[k]
print len(numberGames) + sum

a) 30
b) 24
c) 33
d) 12

 

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

 

Tuples In Python

 

What will be the output of the following Python ??

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

 

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

>>>my_tuple = (1, 2, 3, 4)
>>>my_tuple.append( (5, 6, 7) )
>>>print len(my_tuple)

a) 1
b) 2
c) 5
d) Error

 

What will be the output of the 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

 

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

 

Tuples In Python

 

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

>>>t1 = (1, 2, 4, 3)
>>>t2 = (1, 2, 3, 4)
>>>t1 < t2

a) True
b) False
c) Error
d) None

 

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

 

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

 

What will be the output of the following code ??

>>>t = (1, 2)
>>>2 * t

a) (1, 2, 1, 2)
b) [1, 2, 1, 2]
c) (1, 1, 2, 2)
d) [1, 1, 2, 2]

 

Tuples In Python

 

Tuples can’t be made keys of a dictionary ??

a) True
b) False

 

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

 

What will be the output ??

d = {“john”:40, “peter”:45}
d[“john”]

a) 40
b) 45
c) “john”
d) “peter”

 

Tuples In Python

 

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

 

What will be the output of the 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

 

What will be the output of code ??

>>>t = (1, 2, 4, 3, 8, 9)
>>>[t[i] for i in range(0, len(t), 2)]

a) [2, 3, 9]
b) [1, 2, 4, 3, 8, 9]
c) [1, 4, 8]
d) (1, 4, 8)

Leave a Comment

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

error: Content is protected !!