Files - Python Questions and Answers - Sanfoundry
Files - Python Questions and Answers - Sanfoundry
Files - Python Questions and Answers - Sanfoundry
(//www.sanfoundry.com)
Practice Python questions and answers for interviews, campus placements, online tests, aptitude tests, quizzes and competitive exams.
Get Started
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “List Comprehension – 1”.
advertisement
l=[1,2,3,4,5]
[x&1 for x in l]
a) [1, 1, 1, 1, 1]
b) [1, 0, 1, 0, 1]
c) [1, 0, 0, 0, 0]
d) [0, 1, 0, 1, 0]
View Answer
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 5/15
Answer:
3/27/2019 b List Comprehension Interview Questions and Answers - Sanfoundry
Explanation: In the code shown above, each of the numbers of the list, that is, 1, 2, 3, 4 and 5 are AND-ed with 1 and the result is printed in the form of
a list. Hence the output is [1, 0, 1, 0, 1].
l1=[1,2,3]
l2=[4,5,6]
[x*y for x in l1 for y in l2]
Answer: c
Explanation: The code shown above returns x*y, where x belongs to the list l1 and y belongs to the list l2. Therefore, the output is: [4, 5, 6, 8, 10, 12, 12,
15, 18].
3. Write the list comprehension to pick out only negative integers from a given list ‘l’.
a) [x<0 in l]
b) [x for x<0 in l]
c) [x in l for x<0]
d) [x for x in l if x<0]
View Answer
Answer: d
Explanation: To pick out only the negative numbers from a given list ‘l’, the correct list comprehension statement would be: [x for x in l if x<0].
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 6/15
3/27/2019 List Comprehension Interview Questions and Answers - Sanfoundry
s=["pune", "mumbai", "delhi"]
[(w.upper(), len(w)) for w in s]
a) Error
b) [‘PUNE’, 4, ‘MUMBAI’, 6, ‘DELHI’, 5]
c) [PUNE, 4, MUMBAI, 6, DELHI, 5]
d) [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)]
View Answer
Answer: d
Explanation: If we need to generate two results, we need to put it in the form of a tuple. The code shown above returns each word of list in uppercase,
along with the length of the word. Hence the output of the code is: [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)].
advertisement
l1=[2,4,6]
l2=[-2,-4,-6]
for i in zip(l1, l2):
print(i)
a) 2, -2
4, -4
6, -6
b) [(2, -2), (4, -4), (6, -6)]
c) (2, -2)
(4, -4)
(6, -6)
d) [-4, -16, -36]
View Answer
Answer: c
Explanation: The output of the code shown will be:
(2, -2)
(4, -4)
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 7/15
(6, -6)
3/27/2019 List Comprehension Interview Questions and Answers - Sanfoundry
a) Error
b) 0
c) [-20, -60, -80]
d) [0, 0, 0]
View Answer
Answer: d
Explanation: The code shown above returns x+y, for x belonging to the list l1 and y belonging to the list l2. That is, l3=[10-10, 20-20, 30-20], which is, [0,
0, 0]
7. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].
a) [x**3 for x in l]
b) [x^3 for x in l]
c) [x**3 in l]
d) [x^3 in l]
View Answer
Answer: a
Explanation: The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].
advertisement
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 8/15
3/27/2019 List Comprehension Interview Questions and Answers - Sanfoundry
a) Error
b) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
c) 1 4 7
258
369
d) (1 4 7)
(2 5 8)
(3 6 9)
View Answer
Answer: b
Explanation: In the code shown above, ‘3’ is the index of the list. Had we used a number greater than 3, it would result in an error. The output of this
code is: [[1, 4, 7], [2, 5, 8], [3, 6, 9]].
import math
[str(round(math.pi)) for i in range (1, 6)]
Explanation: The list comprehension shown above rounds off pi(3.141) and returns its value, that is 3. This is done 5 times. Hence the output is: [‘3’, ‘3’,
‘3’, ‘3’, ‘3’].
l1=[1,2,3]
l2=[4,5,6]
l3=[7,8,9]
for x, y, z in zip(l1, l2, l3):
print(x, y, z)
a) 1 4 7
258
369
b) (1 4 7)
(2 5 8)
(3 6 9)
c) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
d) Error
View Answer
Answer: a
Explanation: The output of the code shown above is:
147
258
369
This is due to the statement: print(x, y,z).
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers (https://www.sanfoundry.com/1000-python-
questions-answers/).
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 10/15
3/27/2019 advertisement
List Comprehension Interview Questions and Answers - Sanfoundry
0/10
hours to make sure it’s never dry
SCORE
TRUE FALSE
Sponsored
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 11/15
3/27/2019 List Comprehension Interview Questions and Answers - Sanfoundry
Best Careers
Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel Programming. Our Founder has trained employees of almost all
Top Companies in India such as VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec,
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 12/15
Redhat,
3/27/2019 Chelsio, Cavium, ST-Micro, Samsung, LG-Soft,
List Wipro, TCS,Interview
Comprehension HCL, Questions
IBM, Accenture,
and Answers HSBC, Mphasis, Tata-Elxsi, Tata VSNL, Mindtree,
- Sanfoundry
Best Trainings
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 13/15
3/27/2019 List Comprehension Interview Questions and Answers - Sanfoundry
India Internships
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 14/15
Computer
3/27/2019 Science Internships (https://www.sanfoundry.com/internships-computer-science-engineering/)
List Comprehension Interview Questions and Answers - Sanfoundry
About Sanfoundry
About Us (https://www.sanfoundry.com/about/)
Copyright (https://www.sanfoundry.com/copyright/)
Terms (https://www.sanfoundry.com/tos-privacy-policy/)
Privacy Policy (https://www.sanfoundry.com/privacy-policy/)
Jobs (https://www.sanfoundry.com/jobs/)
Bangalore Training (https://www.sanfoundry.com/coursesfees/)
Online Training (https://www.sanfoundry.com/online-training-san-linux-kernel-device-drivers/)
Developers Track (https://www.sanfoundry.com/salary-50l/)
Mentoring Sessions (https://www.sanfoundry.com/professional-mentoring-coaching-career-guidance-cto/)
Contact Us (https://www.sanfoundry.com/contact/)
Sitemap (https://www.sanfoundry.com/sitemap_index.xml)
https://www.sanfoundry.com/python-questions-answers-list-comprehension-1/ 15/15