Python Questions and Answers Lists 7
Python Questions and Answers Lists 7
sanfoundry.com/python-questions-answers-lists-7
August 6, 2017
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
Explanation: Append function on lists doesn’t return anything. Thus the value of b is
None.
>>> a=[14,52,7]
>>>> b=a.copy()
>>> b is a
a) True
b) False
View Answer
Answer: b
Explanation: 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?
1/7
a=[13,56,17]
a.append([87])
a.extend([45,67])
print(a)
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: (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]
View Answer
2/7
Answer: b
Explanation: 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
Explanation: In the above case, both the lists are equivalent but not identical as they have
different objects.
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
View Answer
Answer: d
Explanation: unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4.
3/7
Thus 5 gets printed.
8. What will be the output of the following Python code?
Answer: b
Explanation: 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]]
View Answer
Answer: a
Explanation: 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
View Answer
Answer: c
Explanation: First, map converts the number to string and then places the individual digits
4/7
in a list. Then, sum finds the sum of the digits in the list. The code basically finds the sum
of digits in the number.
11. What will be the output of the following Python code?
a= [1, 2, 3, 4, 5]
for i in range(1, 5):
a[i-1] = a[i]
for i in range(0, 5):
print(a[i],end = " ")
a) 5 5 1 2 3
b) 5 1 2 3 4
c) 2 3 4 5 1
d) 2 3 4 5 5
View Answer
Answer: d
Explanation: The items having indexes from 1 to 4 are shifted forward by one index due
to the first for-loop and the item of index four is printed again because of the second for-
loop.
12. What will be the output of the following Python code?
a)
3
[44, 2, 3]
b)
1
[1,2,3]
c)
3
[1,2,3]
d)
1
[44,2,3]
View Answer
5/7
Answer: a
Explanation: A list is mutable, hence it’s value changes after function call. However,
integer isn’t mutable. Thus its value doesn’t change.
a = [1, 5, 7, 9, 9, 1]
<br class="blank" />b=a[0]
<br class="blank" />x= 0
for x in range(1, len(a)):
if a[x] > b:
b = a[x]
b= x
print(b)
a) 5
b) 3
c) 4
d) 0
View Answer
Answer: c
Explanation: The above piece of code basically prints the index of the largest element in
the list.
14. What will be the output of the following Python code?
a=["Apple","Ball","Cobra"]
<br class="blank" />a.sort(key=len)
print(a)
Answer: b
Explanation: The syntax isn’t invalid and the list is sorted according to the length of the
strings in the list since key is given as len.
15. What will be the output of the following Python code?
a) 1: 2: 3:
b) Exception is thrown
c) One Two Three
6/7
d) 0: One 1: Two 2: Three
View Answer
Answer: d
Explanation: enumerate(iterator,start=0) is a built-in function which returns (0,lst[0]),
(1,lst[1]) and so on where lst is a list(iterator).
.
7/7