Python MCQs on List: SET 6

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

a=[10,23,56,[78]]

b=list(a)

a[3][0]=95

a[1]=34

print(b)

a) [10,34,56,[95]]

b) [10,23,56,[78]]

c) [10,23,56,[95]]

d) [10,34,56,[78]]

Check Answer

Answer: c
The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.

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

print(list(zip((1,2,3),(‘a’),(‘xxx’,’yyy’))))

print(list(zip((2,4),(‘b’,’c’),(‘yy’,’xx’))))

a)

[(1,2,3),(‘a’),(‘xxx’,’yyy’)] [(2,4),(‘b’,’c’),(‘yy’,’xx’)]

b)

[(1, ‘a’, ‘xxx’),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)] [(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]

c) Syntax error

d)

[(1, ‘a’, ‘xxx’)] [(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]
Check Answer

Answer: d
The zip function combines the individual attributes of the lists into a list of tuples.

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

import copy

a=[10,23,56,[78]]

b=copy.deepcopy(a)

a[3][0]=95

a[1]=34

print(b)

a) [10,34,56,[95]]

b) [10,23,56,[78]]

c) [10,23,56,[95]]

d) [10,34,56,[78]]

Check Answer

Answer: b
The above copy is deepcopy. Any change made in the original list isn’t reflected.

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

s=”[email protected]@[email protected]

a=list(s.partition(“@”))

print(a)

b=list(s.split(“@”,3))

print(b)

a)

[‘a’,’b’,’c’,’d’] [‘a’,’b’,’c’,’d’]

b)

[‘a’,’@’,’b’,’@’,’c’,’@’,’d’] [‘a’,’b’,’c’,’d’]

c)

[‘a’,’@’,’[email protected]@d’] [‘a’,’b’,’c’,’d’]

d)

[‘a’,’@’,’[email protected]@d’] [‘a’,’@’,’b’,’@’,’c’,’@’,’d’]
Check Answer

Answer: c
The partition function only splits for the first parameter along with the separator while split function splits for the number of times given in the second argument but without the separator.

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

a=[1,2,3,4]

b=[sum(a[0:x+1]) for x in range(0,len(a))]

print(b)

a) 10

b) [1,3,5,7]

c) 4

d) [1,3,6,10]

Check Answer

Answer: d
The above code returns the cumulative sum of elements in a list.

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

a=”hello”

b=list((x.upper(),len(x)) for x in a)

print(b)

a) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1)]

b) [(‘HELLO’, 5)]

c) [(‘H’, 5), (‘E’, 5), (‘L’, 5), (‘L’, 5), (‘O’, 5)]

d) Syntax error

Check Answer

Answer: a
Variable x iterates over each letter in string a hence the length of each letter is 1.

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

a=[1,2,3,4]

b=[sum(a[0:x+1]) for x in range(0,len(a))]

print(b)

a) 10

b) [1,3,5,7]

c) 4

d) [1,3,6,10]

Check Answer

Answer: d
The above code returns the cumulative sum of elements in a list.

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

a=[[]]*3

a[1].append(7)

print(a)

a) Syntax error

b) [[7], [7], [7]]

c) [[7], [], []]

d) [[],7, [], []]

Check Answer

Answer: b
The first line of the code creates multiple reference copies of sublist. Hence when 7 is appended, it gets appended to all the sublists.

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

b=[2,3,4,5]

a=list(filter(lambda x:x%2,b))

print(a)

a) [2,4]

b) [ ]

c) [3,5]

d) Invalid arguments for filter function

Check Answer

Answer: c
The filter function gives value from the list b for which the condition is true, that is, x%2==1.

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

lst=[3,4,6,1,2]

lst[1:2]=[7,8]

print(lst)

a) [3, 7, 8, 6, 1, 2]

b) Syntax error

c) [3,[7,8],6,1,2]

d) [3,4,6,7,8]

Check Answer

Answer: a
In the piece of code, slice assignment has been implemented. The sliced list is replaced by the assigned elements in the list. Type in python shell to verify.

Pages  1  2  3  4  5  6  7

Leave a Comment