Python Multiple Choice Quizs Question – While and For Loops 1

Q 1. 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

Show Answer Answer:-C. error
Explanation SyntaxError, there shouldn’t be a space between + and = in +=.

Q 2. 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 7
B. 1 2 3 4 5 6
C. error
D. none of the mentioned

Show Answer Answer:-B. 1 2 3 4 5 6
Explanation Control exits the loop when i becomes 7.

Q 3. 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
C. 5 6 7 8
D. error

Show Answer Answer:-C. 5 6 7 8
Explanation 0O11 is an octal number.

Q 4. 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

Show Answer Answer:-D. error
Explanation 9 isn’t allowed in an octal number.

Q 5. 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 …

Show Answer Answer:-D. 1 3 5 7 9 11 …
Explanation The loop does not terminate since i is never an even number.

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

i = 2

whileTrue:

if i%3 == 0:

break

print(i)

i += 2

A. 2 4 6 8 10 …
B. 2 3 4
C. 2 4
D. error

Show Answer Answer:-C. 2 4
Explanation 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.

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

Show Answer Answer:-D. none of the mentioned
Explanation Control does not enter the loop because of False.

Q 8. 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

Show Answer Answer:-D. none of the mentioned
Explanation SyntaxError, True is a keyword and it’s value cannot be changed.

Q 9. 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

Show Answer Answer:-D. none of the mentioned
Explanation The loop does not terminate as new elements are being added to the list in each iteration.

Q 10 . 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

Show Answer Answer:-B. [‘ab’, ‘cd’]
Explanation The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.

Comments

Leave a Reply

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

You cannot copy content of this page