This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Lists-6”.
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)
a) [10,34,56,[95]]
b) [10,23,56,[78]]
c) [10,23,56,[95]]
d) [10,34,56,[78]]
View Answer
Explanation: The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.
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'))))
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')]View Answer
Explanation: The zip function combines the individual attributes of the lists into a list of tuples.
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)
a) [10,34,56,[95]]
b) [10,23,56,[78]]
c) [10,23,56,[95]]
d) [10,34,56,[78]]
View Answer
Explanation: The above copy is deepcopy. Any change made in the original list isn’t reflected.
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)
a)
[‘a’,’b’,’c’,’d’] [‘a’,’b’,’c’,’d’]
b)
[‘a’,’@’,’b’,’@’,’c’,’@’,’d’] [‘a’,’b’,’c’,’d’]
c)
[‘a’,’@’,’b@c@d’] [‘a’,’b’,’c’,’d’]
d)
[‘a’,’@’,’b@c@d’] [‘a’,’@’,’b’,’@’,’c’,’@’,’d’]View Answer
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.
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)
a) 10
b) [1,3,5,7]
c) 4
d) [1,3,6,10]
View Answer
Explanation: The above code returns the cumulative sum of elements in a list.
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)
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
View Answer
Explanation: Variable x iterates over each letter in string a hence the length of each letter is 1.
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)
a) 10
b) [1,3,5,7]
c) 4
d) [1,3,6,10]
View Answer
Explanation: The above code returns the cumulative sum of elements in a list.
8. 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, [], []]
View Answer
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.
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)
a) [2,4]
b) [ ]
c) [3,5]
d) Invalid arguments for filter function
View Answer
Explanation: The filter function gives value from the list b for which the condition is true, that is, x%2==1.
10. 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]
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Practice Programming MCQs
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books