Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists-7”.
Q 1.What will be the output of the following Python code?
a=["Apple","Ball","Cobra"]
a.sort(key=len) print(a)
A. Invalid syntax for sort()
B. [‘Ball’, ‘Apple’, ‘Cobra’]
C.[‘Apple’, ‘Ball’, ‘Cobra’]
D.[‘Cobra’, ‘Apple’, ‘Ball’]
Show Answer
Answer:-B.[‘Ball’, ‘Apple’, ‘Cobra’]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.Q 2. What will be the output of the following Python code?
a = [1, 5, 7, 9, 9, 1]
b=a[0]
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
Show Answer
Answer:-C. 4Explanation
The above piece of code basically prints the index of the largest element in the list.Q 3. What will be the output of the following Python code?
def change(var, lst): var = 1 lst[0] = 44 k = 3 a = [1, 2, 3] change(k, a) print(k) print(a)
A.3
[44, 2, 3]
B.3
[1,2,3]
C.1
[44,2,3]
D. 1
[1,2,3]
Show Answer
Answer:-A. 3 [44, 2, 3]Explanation
A list is mutable, hence it’s value changes after function call. However, integer isn’t mutable. Thus its value doesn’t change.Q 4. 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 1 2 3 4
B. 5 5 1 2 3
C. 2 3 4 5 5
D. 2 3 4 5 1
Show Answer
Answer:- C. 2 3 4 5 5Explanation
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.Q 5. What will be the output of the following Python code?
a=165 b=sum(list(map(int,str(a)))) print(b)
A. 12
B. 23
C. 54
D. Syntax error
Show Answer
Answer:-A. 12Explanation
First, map converts the number to string and then places the individual digits 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.Q 6. What will be the output of the following Python code?
x=[[1],[2]] print(" ".join(list(map(str,x))))
A. [49] [50]
B. [1] [2]
C. [[1]] [[2]]
D. Syntax error
Show Answer
Answer:- B. [1] [2]Explanation
The elements 1 and 2 are first put into separate lists and then combined with a space in between using the join attribute.Q 7. What will be the output of the following Python code?
places = ['Bangalore', 'Mumbai', 'Delhi']
places1 = places places2 = places[:]
places1[1]="Pune" places2[2]="Hyderabad" print(places)
A.[‘Bangalore’, ‘Mumbai’, ‘Delhi’]
B. [‘Bangalore’, ‘Mumbai’, ‘Hyderabad’]
C. [‘Bangalore’, ‘Pune’, ‘Delhi’]
D.[‘Bangalore’, ‘Pune’, ‘Hyderabad’]
Show Answer
Answer:-C. [‘Bangalore’, ‘Pune’, ‘Delhi’]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.Q 8. 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. 5
B.[5]
C. [1,4]
D. Error
Show Answer
Answer:-A. 5Explanation
unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4. Thus 5 gets printed.Q 9. 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. True
False
C. False
False
D. False
True
Show Answer
Answer:-B. True FalseExplanation
In the above case, both the lists are equivalent but not identical as they have different objects.Q 10. What will be the output of the following Python code?
lst=[[1,2],[3,4]] print(sum(lst,[]))
A.[10]
B.[[3],[7]]
C.[1,2,3,4]
D. Error