Python MCQs on While and For Loops: SET 5

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

for i in range(0):

print(i)

a) 0

b) no output

c) error

d) none of the mentioned

Check Answer

Answer: b
range(0) is empty.

 

 

 

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

for i in range(2.0):

print(i)

a) 0.0 1.0

b) 0 1

c) error

d) none of the mentioned

Check Answer

Answer: c
Object of type float cannot be interpreted as an integer.

 

 

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

for i in range(int(2.0)):

print(i)

a) 0.0 1.0

b) 0 1

c) error

d) none of the mentioned

Check Answer

Answer: b
range(int(2.0)) is the same as range(2).

 

 

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

for i in range(float(‘inf’)):

print (i)

a) 0.0 0.1 0.2 0.3 …

b) 0 1 2 3 …

c) 0.0 1.0 2.0 3.0 …

d) none of the mentioned

Check Answer

Answer: d
Error, objects of type float cannot be interpreted as an integer.

 

 

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

for i in range(int(float(‘inf’))):

print (i)

a) 0.0 0.1 0.2 0.3 …

b) 0 1 2 3 …

c) 0.0 1.0 2.0 3.0 …

d) none of the mentioned

Check Answer

Answer: d
OverflowError, cannot convert float infinity to integer.

 

 

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

for i in [1, 2, 3, 4][::-1]:

print (i)

a) 1 2 3 4

b) 4 3 2 1

c) error

d) none of the mentioned

Check Answer

Answer: b
[::-1] reverses the list.

 

 

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

for i in ”.join(reversed(list(‘abcd’))):

print (i)

a) a b c d

b) d c b a

c) error

d) none of the mentioned

Check Answer
Answer: b
‘ ‘.join(reversed(list(‘abcd’))) reverses a string.

 

 

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

for i in ‘abcd'[::-1]:

print (i)

a) a b c d

b) d c b a

c) error

d) none of the mentioned

Check Answer

Answer: b
[::-1] reverses the string.

 

 

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

for i in ”:

print (i)

a) None

b) (nothing is printed)

c) error

d) none of the mentioned

Check Answer

Answer: b
The string does not have any character to loop over.

 

 

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

x = 2

for i in range(x):

x += 1

print (x)

a) 0 1 2 3 4 …

b) 0 1

c) 3 4

d) 0 1 2 3

Check Answer

Answer: c
Variable x is incremented and printed twice.

 

Page  1  2  3  4  5  6

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