Python MCQs on List Comprehension: SET 2

11. What will be the output of the following Python code?

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]

Check Answer
Answer: b
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].

 

12. What will be the output of the following Python code?

l1=[1,2,3]

l2=[4,5,6]

[x*y for x in l1 for y in l2]

a) [4, 8, 12, 5, 10, 15, 6, 12, 18]

b) [4, 10, 18]

c) [4, 5, 6, 8, 10, 12, 12, 15, 18]

d) [18, 12, 6, 15, 10, 5, 12, 8, 4]

Check Answer
Answer: c
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].

 

13. 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]

Check Answer
Answer: d
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].

For example if we have a list l=[-65, 2, 7, -99, -4, 3] >>> [x for x in l if x<0] The output would be: [-65, -99, -4].

 

14. What will be the output of the following Python code?

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)]

Check Answer
Answer: d
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)].

 

15. What will be the output of the following Python code?

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]

Check Answer
Answer: c
The output of the code shown will be:
(2, -2)
(4, -4)
(6, -6)
This format is due to the statement print(i).

 

16. What will be the output of the following Python code?

l1=[10, 20, 30]

l2=[-10, -20, -30]

l3=[x+y for x, y in zip(l1, l2)]

l3

a) Error

b) 0

c) [-20, -60, -80]

d) [0, 0, 0]

Check Answer
Answer: d
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].

 

17. 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]

Check Answer
Answer: a
The list comprehension to print a list of cube of the numbers for the given list is: [x**3 for x in l].

 

18. What will be the output of the following Python code?

l=[[1 ,2, 3], [4, 5, 6], [7, 8, 9]]

[[row[i] for row in l] for i in range(3)]

a) Error

b) [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

c)

1 4 7
2 5 8
3 6 9

d)

(1 4 7)
(2 5 8)
(3 6 9)

Check Answer

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

 

19. What will be the output of the following Python code?

import math

[str(round(math.pi)) for i in range (1, 6)]

a) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’, ‘3’]

b) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’, ‘3.141582’]

c) [‘3’, ‘3’, ‘3’, ‘3’, ‘3’]

d) [‘3.1’, ‘3.14’, ‘3.142’, ‘3.1416’, ‘3.14159’]

Check Answer
Answer: c
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’].

 

20. What will be the output of the following Python code?

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
2 5 8
3 6 9

b)

(1 4 7)
(2 5 8)
(3 6 9)

c) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

d) Error

Check Answer
Answer: a
The output of the code shown above is:
1 4 7
2 5 8
3 6 9
This is due to the statement: print(x, y,z).

 

Pages  1  2  3  4

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment