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