Python Questions and Answers – Lists – 6
Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists-6”.
Q 1. 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)
Show Answer
Answer:-B. [10,23,56,[95]]Explanation
The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.Q 2. 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'))))
Show Answer
Answer:-C. [(1, ‘a’, ‘xxx’)] [(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]Explanation
The zip function combines the individual attributes of the lists into a list of tuples.Q 3. 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)
Show Answer
Answer:-B. [10,23,56,[78]]Explanation
The above copy is deepcopy. Any change made in the original list isn’t reflected.Q 4. What will be the output of the following Python code?
s="a@b@c@d"
a=list(s.partition("@"))
print(a)
b=list(s.split("@",3))
print(b)
Show Answer
Answer:-C. [‘a’,’@’,’b@c@d’] [‘a’,’b’,’c’,’d’]Explanation
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.Q 5. 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)
Show Answer
Answer:-D.[1,3,6,10]Explanation
The above code returns the cumulative sum of elements in a list.Q 6. What will be the output of the following Python code?
a="hello" b=list((x.upper(),len(x)) for x in a) print(b)
Show Answer
Answer:-A. [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1)]Explanation
Variable x iterates over each letter in string a hence the length of each letter is 1.Q 7. 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)
Show Answer
Answer:-C. [1,3,6,10]Explanation
The above code returns the cumulative sum of elements in a list.Q 8. What will be the output of the following Python code?
a=[[]]*3 a[1].append(7) print(a)
Show Answer
Answer:-B. [[7], [7], [7]]Explanation
The first line of the code creates multiple reference copies of sublist. Hence when 7 is appended, it gets appended to all the sublists.Q 9. 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)
Show Answer
Answer:-B. [3,5]Explanation
The filter function gives value from the list b for which the condition is true, that is, x%2==1.Q 10. What will be the output of the following Python code?
lst=[3,4,6,1,2] lst[1:2]=[7,8] print(lst)