Python MCQs on List Comprehension: SET 1

Python MCQs on List Comprehension

In this section, we are going to introduce you to the most frequently asked Python MCQs on List Comprehension. These Python MCQs on List Comprehension will be going to help you in Interviews and Exams.

 

1. What will be the output of the following Python code snippet?

k = [print(i) for i in my_string if i not in “aeiou”]

a) prints all the vowels in my_string

b) prints all the consonants in my_string

c) prints all characters of my_string that aren’t vowels

d) prints only on executing print(k)

Check Answer

Answer: c
print(i) is executed if the given character is not a vowel.

 

2. What is the output of print(k) in the following Python code snippet?

k = [print(i) for i in my_string if i not in “aeiou”]

print(k)

a) all characters of my_string that aren’t vowels

b) a list of Nones

c) list of Trues

d) list of Falses

Check Answer

Answer: b
print() returns None.

 

3. What will be the output of the following Python code snippet?

my_string = “hello world”
k = [(i.upper(), len(i)) for i in my_string]
print(k)

a) [(‘HELLO’, 5), (‘WORLD’, 5)]

b) [(‘H’, 1), (‘E’, 1), (‘L’, 1), (‘L’, 1), (‘O’, 1), (‘ ‘, 1), (‘W’, 1), (‘O’, 1), (‘R’, 1), (‘L’, 1), (‘D’, 1)]

c) [(‘HELLO WORLD’, 11)]

d) none of the mentioned

Check Answer

Answer: b
We are iterating over each letter in the string.

 

4. Which of the following is the correct expansion of list_1 = [expr(i) for i in list_0 if func(i)]?

a)

list_1 = [] for i in list_0:
if func(i):
list_1.append(i)

b)

for i in list_0:
if func(i):
list_1.append(expr(i))

c)

list_1 = [] for i in list_0:
if func(i):
list_1.append(expr(i))

d) none of the mentioned

Check Answer

Answer: c
We have to create an empty list, loop over the contents of the existing list and check if a condition is satisfied before performing some operation and adding it to the new list.

 

5. What will be the output of the following Python code snippet?

x = [i**+1 for i in range(3)]; print(x);

a) [0, 1, 2]

b) [1, 2, 5]

c) error, **+ is not a valid operator

d) error, ‘;’ is not allowed

Check Answer

Answer: a
i**+1 is evaluated as (i)**(+1).

 

6. What will be the output of the following Python code snippet?

print([i.lower() for i in “HELLO”])

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

b) ‘hello’

c) [‘hello’]

d) hello

Check Answer

Answer: a
We are iterating over each letter in the string.

 

7. What will be the output of the following Python code snippet?

print([i+j for i in “abc” for j in “def”])

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]

b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]

c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]

d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]

Check Answer

Answer: d

If it were to be executed as a nested for loop, i would be the outer loop and j the inner loop.

 

8. What will be the output of the following Python code snippet?

print([[i+j for i in “abc”] for j in “def”])

a) [‘da’, ‘ea’, ‘fa’, ‘db’, ‘eb’, ‘fb’, ‘dc’, ‘ec’, ‘fc’]

b) [[‘ad’, ‘bd’, ‘cd’], [‘ae’, ‘be’, ‘ce’], [‘af’, ‘bf’, ‘cf’]]

c) [[‘da’, ‘db’, ‘dc’], [‘ea’, ‘eb’, ‘ec’], [‘fa’, ‘fb’, ‘fc’]]

d) [‘ad’, ‘ae’, ‘af’, ‘bd’, ‘be’, ‘bf’, ‘cd’, ‘ce’, ‘cf’]

Check Answer
Answer: b
The inner list is generated once for each value of j.

 

9. What will be the output of the following Python code snippet?

print([if i%2==0: i; else: i+1; for i in range(4)])

a) [0, 2, 2, 4]

b) [1, 1, 3, 3]

c) error

d) none of the mentioned

Check Answer
Answer: c
Syntax error.

 

10. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?

a) [x**-1 for x in [(1, 2, 3)]]

b) [1/x for x in [(1, 2, 3)]]

c) [1/x for x in (1, 2, 3)]

d) error

Check Answer
Answer: c
x**-1 is evaluated as (x)**(-1).

 

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