Engineering Questions with Answers - Multiple Choice Questions

Python MCQs

1 - Question

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)

View Answer

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




2 - Question

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

View Answer

Answer: b
Explanation: print() returns None.




3 - Question

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

View Answer

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




4 - Question

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

View Answer

Answer: c
Explanation: 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 - Question

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

View Answer

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




6 - Question

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

View Answer

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




7 - Question

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

View Answer

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




8 - Question

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

View Answer

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




9 - Question

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

View Answer

Answer: c
Explanation: Syntax error.




10 - Question

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

View Answer

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

Get weekly updates about new MCQs and other posts by joining 18000+ community of active learners