Python Questions and Answers – Lists – 6

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

Answer: c
Explanation: The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.
advertisement
advertisement

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’)]
Note: Join free Sanfoundry classes at Telegram or Youtube

b)

[(1, 'a', 'xxx'),(2,’ ‘,’yyy’),(3,’ ‘,’ ‘)]
[(2, 'b', 'yy'), (4, 'c', 'xx')]
advertisement

c) Syntax error
d)

[(1, 'a', 'xxx')]
[(2, 'b', 'yy'), (4, 'c', 'xx')]
View Answer
Answer: d
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?

advertisement
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

Answer: b
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
Answer: c
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

Answer: d
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

Answer: a
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

Answer: d
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

Answer: b
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

Answer: c
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

Answer: a
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.