Python Multiple Choice Questions on “Lists-7”.
1. What will be the output of the following Python code?
a=[1,2,3] b=a.append(4) print(a) print(b)
a)
[1,2,3,4] [1,2,3,4]
b)
[1, 2, 3, 4] None
c) Syntax error
d)
[1,2,3] [1,2,3,4]
View Answer
Answer: b
Clarification: Append function on lists doesn’t return anything. Thus the value of b is None.
2. What will be the output of the following Python code?
>>> a=[14,52,7] >>>> b=a.copy() >>> b is a
a) True
b) False
Answer: b
Clarification: List b is just a copy of the original list. Any copy made in list b will not be reflected in list a.
3. What will be the output of the following Python code?
a=[13,56,17] a.append([87]) a.extend([45,67]) print(a)
a) [13, 56, 17, [87], 45, 67]
b) [13, 56, 17, 87, 45, 67]
c) [13, 56, 17, 87,[ 45, 67]]
d) [13, 56, 17, [87], [45, 67]]
Answer: a
Clarification: The append function simply adds its arguments to the list as it is while extend function extends its arguments and later appends it.
4. What is the output of the following piece of code?
a=list((45,)*4) print((45)*4) print(a)
a)
180 [(45),(45),(45),(45)]
b)
(45,45,45,45) [45,45,45,45]
c)
180 [45,45,45,45]
d) Syntax error
Answer: c
Clarification: (45) is an int while (45,) is a tuple of one element. Thus when a tuple is multiplied, it created references of itself which is later converted to a list.
5. What will be the output of the following Python code?
lst=[[1,2],[3,4]] print(sum(lst,[]))
a) [[3],[7]]
b) [1,2,3,4]
c) Error
d) [10]
Answer: b
Clarification: The above piece of code is used for flattening lists.
6. What will be the output of the following Python code?
word1="Apple" word2="Apple" list1=[1,2,3] list2=[1,2,3] print(word1 is word2) print(list1 is list2)
a)
True True
b)
False True
c)
False False
d)
True False
View Answer
Answer: d
Clarification: In the above case, both the lists are equivalent but not identical as they have different objects.
7. What will be the output of the following Python code?
def unpack(a,b,c,d): print(a+d) x = [1,2,3,4] unpack(*x)
a) Error
b) [1,4]
c) [5]
d) 5
Answer: d
Clarification: unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4. Thus 5 gets printed.
8. What will be the output of the following Python code?
places = ['Bangalore', 'Mumbai', 'Delhi'] <br class="blank" />places1 = places places2 = places[:] <br class="blank" />places1[1]="Pune" places2[2]="Hyderabad" print(places)
a) [‘Bangalore’, ‘Pune’, ‘Hyderabad’]
b) [‘Bangalore’, ‘Pune’, ‘Delhi’]
c) [‘Bangalore’, ‘Mumbai’, ‘Delhi’]
d) [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]
Answer: b
Clarification: places1 is an alias of the list places. Hence, any change made to places1 is reflected in places. places2 is a copy of the list places. Thus, any change made to places2 isn’t reflected in places.
9. What will be the output of the following Python code?
x=[[1],[2]] print(" ".join(list(map(str,x))))
a) [1] [2]
b) [49] [50]
c) Syntax error
d) [[1]] [[2]]
Answer: a
Clarification: The elements 1 and 2 are first put into separate lists and then combined with a space in between using the join attribute.
10. What will be the output of the following Python code?
a=165 b=sum(list(map(int,str(a)))) print(b)
a) 561
b) 5
c) 12
d) Syntax error
