Python MCQs on List Comprehension: SET 3

21. Read the information given below carefully and write a list comprehension such that the output is: [‘e’, ‘o’]

w=”hello”
v=(‘a’, ‘e’, ‘i’, ‘o’, ‘u’)

a) [x for w in v if x in v]

b) [x for x in w if x in v]

c) [x for x in v if w in v]

d) [x for v in w for x in w]

Check Answer
Answer: b
The tuple ‘v’ is used to generate a list containing only vowels in the string ‘w’. The result is a list containing only vowels present in the string “hello”. Hence the required list comprehension is: [x for x in w if x in v].

 

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

[ord(ch) for ch in ‘abc’]

a) [97, 98, 99]

b) [‘97’, ‘98’, ‘99’]

c) [65, 66, 67]

d) Error

Check Answer
Answer: a
The list comprehension shown above returns the ASCII value of each alphabet of the string ‘abc’. Hence the output is: [97, 98, 99]. Had the string been ‘ABC’, the output would be: [65, 66, 67].

 

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

t=32.00
[round((x-32)*5/9) for x in t]

a) [0]

b) 0

c) [0.00]

d) Error

Check Answer
Answer: d
The value of t in the code shown above is equal to 32.00, which is a floating point value. ‘Float’ objects are not iterable. Hence the code results in an error.

 

24. Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.

a) [x in range(1, 1000) if x%3==0]

b) [x for x in range(1000) if x%3==0]

c) [x%3 for x in range(1, 1000)]

d) [x%3=0 for x in range(1, 1000)]

Check Answer
Answer: b
The list comprehension [x for x in range(1000) if x%3==0] produces a list of numbers between 1 and 1000 that are divisible by 3.

 

25. Write a list comprehension equivalent for the Python code shown below.

for i in range(1, 101):
if int(i*0.5)==i*0.5:
print(i)

a) [i for i in range(1, 100) if int(i*0.5)==(i*0.5)] b) [i for i in range(1, 101) if int(i*0.5)==(i*0.5)] c) [i for i in range(1, 101) if int(i*0.5)=(i*0.5)] d) [i for i in range(1, 100) if int(i*0.5)=(i*0.5)]

Check Answer
Answer: b
The code shown above prints the value ‘i’ only if it satisfies the condition: int(i*0.5) is equal to (i*0.5). Hence the required list comprehension is: [i for i in range(1, 101) if int(i*0.5)==(i*0.5)].

 

26. What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))?

a) [1|x for x in [1, 2, 3]]

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

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

d) [x^-1 for x in range(4)]

Check Answer
Answer: c
The output of the function list(map(lambda x:x**-1, [1, 2, 3])) is [1.0, 0.5, 0.3333333333333333] and that of the list comprehension [x**-1 for x in [1, 2, 3]] is [1.0, 0.5, 0.3333333333333333]. Hence the answer is: [x**-1 for x in [1, 2, 3]].

 

27. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].

a) [(2**x) for x in range(0, 13)]

b) [(x**2) for x in range(1, 13)]

c) [(2**x) for x in range(1, 13)]

d) [(x**2) for x in range(0, 13)]

Check Answer
Answer: a
The required list comprehension will print the numbers from 1 to 12, each raised to 2. The required answer is thus, [(2**x) for x in range(0, 13)].

 

28. What is the list comprehension equivalent for?

{x : x is a whole number less than 20, x is even} (including zero)

a) [x for x in range(1, 20) if (x%2==0)]

b) [x for x in range(0, 20) if (x//2==0)]

c) [x for x in range(1, 20) if (x//2==0)]

d) [x for x in range(0, 20) if (x%2==0)]

Check Answer
Answer: d
The required list comprehension will print a whole number, less than 20, provided that the number is even. Since the output list should contain zero as well, the answer to this question is: [x for x in range(0, 20) if (x%2==0)].

 

29. What will be the output of the following Python list comprehension?

[j for i in range(2,8) for j in range(i*2, 50, i)]

a) A list of prime numbers up to 50

b) A list of numbers divisible by 2, up to 50

c) A list of non prime numbers, up to 50

d) Error

Check Answer
Answer: c
The list comprehension shown above returns a list of non-prime numbers up to 50. The logic behind this is that the square root of 50 is almost equal to 7. Hence all the multiples of 2-7 are not prime in this range.

 

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

l=[“good”, “oh!”, “excellent!”, “#450”]
[n for n in l if n.isalpha() or n.isdigit()]

a) [‘good’, ‘oh’, ‘excellent’, ‘450’ ]

b) [‘good’]

c) [‘good’, ‘#450’]

d) [‘oh!’, ‘excellent!’, ‘#450’]

Check Answer
Answer: b
The code shown above returns a new list containing only strings which do not have any punctuation in them. The only string from the list which does not contain any punctuation is ‘good’. Hence the output of the code shown above is [‘good’].

 

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