250+ TOP MCQs on Python List Comprehension and Answers

Python Multiple Choice Questions on “List Comprehension”.

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

  250+ TOP MCQs on Python Core Data types and Answers

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
Answer: a
Clarification: 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
Answer: a
Clarification: 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’]
Answer: d
Clarification: 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’]
Answer: b
Clarification: The inner list is generated once for each value of j.

  250+ TOP MCQs on Python Dictionary and Answers

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
Answer: c
Clarification: 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
Answer: c
Clarification: x**-1 is evaluated as (x)**(-1).

Leave a Comment

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

Scroll to Top