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

Python Multiple Choice Questions on “While and For Loops”.

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

x = ['ab', 'cd']
for i in x:
    i.upper()
print(x)

a) [‘ab’, ‘cd’]
b) [‘AB’, ‘CD’]
c) [None, None]
d) none of the mentioned
Answer: a
Clarification: The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.

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

x = ['ab', 'cd']
for i in x:
    x.append(i.upper())
print(x)

a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’]
c) [‘ab’, ‘cd’]
d) none of the mentioned
Answer: d
Clarification: The loop does not terminate as new elements are being added to the list in each iteration.

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

i = 1
while True:
    if i%3 == 0:
        break
    print(i)
 
    i + = 1

a) 1 2
b) 1 2 3
c) error
d) none of the mentioned
Answer: c
Clarification: SyntaxError, there shouldn’t be a space between + and = in +=.

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

i = 1
while True:
    if i%0O7 == 0:
        break
    print(i)
    i += 1

a) 1 2 3 4 5 6
b) 1 2 3 4 5 6 7
c) error
d) none of the mentioned
Answer: a
Clarification: Control exits the loop when i becomes 7.

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

i = 5
while True:
    if i%0O11 == 0:
        break
    print(i)
    i += 1

a) 5 6 7 8 9 10
b) 5 6 7 8
c) 5 6
d) error
Answer: b
Clarification: 0O11 is an octal number.

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

i = 5
while True:
    if i%0O9 == 0:
        break
    print(i)
    i += 1

a) 5 6 7 8
b) 5 6 7 8 9
c) 5 6 7 8 9 10 11 12 13 14 15 ….
d) error

Answer: d
Clarification: 9 isn’t allowed in an octal number.

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

i = 1
while True:
    if i%2 == 0:
        break
    print(i)
    i += 2

a) 1
b) 1 2
c) 1 2 3 4 5 6 …
d) 1 3 5 7 9 11 …
Answer: d
Clarification: The loop does not terminate since i is never an even number.

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

i = 2
while True:
    if i%3 == 0:
        break
    print(i)
    i += 2

a) 2 4 6 8 10 …
b) 2 4
c) 2 3
d) error
Answer: b
Clarification: The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence control exits the loop.

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

i = 1
while False:
    if i%2 == 0:
        break
    print(i)
    i += 2

a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned
Answer: d
Clarification: Control does not enter the loop because of False.

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

True = False
while True:
    print(True)
    break

a) True
b) False
c) None
d) none of the mentioned
Answer: d
Clarification: SyntaxError, True is a keyword and it’s value cannot be changed.

Leave a Comment

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

Scroll to Top