250+ TOP MCQs on Python While and For Loops and Answers

Python Questions and Answers for Experienced people on “While and For Loops”.

1. 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
Answer: c
Clarification: Object of type float cannot be interpreted as an integer.

2. 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
Answer: b
Clarification: range(int(2.0)) is the same as range(2).

3. 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
Answer: d
Clarification: Error, objects of type float cannot be interpreted as an integer.

4. 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
Answer: d
Clarification: OverflowError, cannot convert float infinity to integer.

5. 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
Answer: b
Clarification: [::-1] reverses the list.

6. 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

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

7. 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
Answer: b
Clarification: [::-1] reverses the string.

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

a) None
b) (nothing is printed)
c) error
d) none of the mentioned
Answer: b
Clarification: The string does not have any character to loop over.

9. 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
Answer: c
Clarification: Variable x is incremented and printed twice.

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

x = 2
for i in range(x):
    x -= 2
    print (x)

a) 0 1 2 3 4 …
b) 0 -2
c) 0
d) error
Answer: b
Clarification: The loop is entered twice.

questions on Python for experienced people,

Leave a Comment

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

Scroll to Top