Python Multiple Choice Questions & Answers (MCQs) focuses on “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)
A. [10,34,56,[78]]
B. [10,23,56,[95]]
C. [10,23,56,[78]]
D. [10,34,56,[95]]
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'))))
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. [(1, ‘a’, ‘xxx’)] [(2, ‘b’, ‘yy’), (4, ‘c’, ‘xx’)]
D. Syntax error
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)
A. [10,34,56,[95]]
B. [10,23,56,[78]]
C. [10,23,56,[95]]
D. [10,34,56,[78]]
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)
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’]
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)
A. 10
B. [1,3,5,7]
C. 4
D.[1,3,6,10]
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)
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
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)
A. [1,3,6,1]
B.[1,3,5,7]
C.[1,3,6,9]
D.[1,3,6,10]
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)
A. Syntax error
B.[[7], [7], [7]]
C. [[7], [], []]
D.[[],7, [], []]
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)
A.[2,4]
B. [3,5]
C. [ ]
D. Invalid arguments for filter function
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)
A. [3,4,6,7,8]
B. [3, 7, 8, 6, 1, 2]
C. [3,[7,8],6,1,2]
D. Syntax error
Show Answer Answer:-B. [3, 7, 8, 6, 1, 2]
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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Author

quizsquestion@gmail.com

Related Posts

Python Questions and Answers – Exception Handling – 3

Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 3”. Q 1. What happens if the file is not...

Read out all

Python Questions and Answers – Exception Handling – 2

Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”. Q 1. The following Python code will result in...

Read out all

C++ Programming Questions and Answers – Exception Handling – 2

C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 2”. Q 1. Where should we place catch block...

Read out all

C++ Programming Questions and Answers – Exceptions That Are Not Errors

C++ MCQs (multiple choice questions) focuses on “Exceptions That Are Not Errors”. One shall practice these MCQs to improve their C++ programming...

Read out all

Python Questions and Answers – Encapsulation

Python Multiple Choice Questions & Answers (MCQs) focuses on “Encapsulation”. Q 1. Which of these is not a fundamental features of OOP?A....

Read out all

You cannot copy content of this page