Python Quizs Question and Answers – While and For Loops – 2

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

x = “abcdef”

while i in x:

print(i, end=” “)

A. a b c d e f
B. abcdef
C. i i i i i i …
D. error

Show Answer Answer:-D. error
Explanation NameError, i is not defined.

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

x = “abcdef”

i = “i”

while i in x:

print(i, end=” “)

A. a b c d e f
B. i i i i i i …
C. no output
D. abcdef

Show Answer Answer:-C. no output
Explanation “i” is not in “abcdef”

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

x = “abcdef”

i = “a”

while i in x:

print(i, end = ” “)

A. no output
B. i i i i i i …
C. a a a a a a …
D. a b c d e f

Show Answer Answer:-C. a a a a a a …
Explanation As the value of i or x isn’t changing, the condition will always evaluate to True.

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

x = “abcdef”

i = “a”

while i in x:

print(‘i’, end = ” “)

A. no output
B. a b c d e f
C. a a a a a a …
D. i i i i i i …

Show Answer Answer:-D. i i i i i i …
Explanation Here i i i i i … printed continuously because as the value of i or x isn’t changing, the condition will always evaluate to True. But also here we use a citation marks on “i”, so, here i treated as a string, not like a variable.

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

x = “abcdef”

i = “a”

while i in x:

x = x[:-1]

print(i, end = ” “)

A. i i i i i i
B. a a a a a
C. a a a a a a
D. none of the mentioned

Show Answer Answer:-C. a a a a a a
Explanation The string x is being shortened by one character in each iteration.

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

x = “abcdef”

i = “a”

while i in x[:-1]:

print(i, end = ” “)

A. a a a a a
B. a a a a a a
C. a a a a a a …
D. a

Show Answer Answer:-C. a a a a a a …
Explanation String x is not being altered and i is in x[:-1].

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

x = “abcdef”

i = “a”

while i in x: x = x[1:]

print(i, end = ” “)

A. a
B. a a a a a a
C. no output
D. error

Show Answer Answer:-A. a
Explanation The string x is being shortened by one character in each iteration.

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

x = “abcdef”

i = “a”

while i in x[1:]:

print(i, end = ” “)

A. a a a a a a
B. a
C. no output
D. error

Show Answer Answer:-C. no output
Explanation i is not in x[1:].

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

i = 0

while i < 3:

print(i) i += 1

else:

print(0)

A. 0 1 2 3 0
B. 0 1 2 0
C. 0 1 2
D. error

Show Answer Answer:-B. 0 1 2 0
Explanation The else part is executed when the condition in the while statement is false.

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

i = 0

while i < 5:

print(i)

i += 1

if i == 3:

break

else:

print(0)

A. 0 1 2
B. 0 1 2 0
C. error
D. none of the mentioned

Show Answer Answer:-A. 0 1 2
Explanation The else part is not executed if control breaks out of the loop.

Comments

Leave a Reply

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

You cannot copy content of this page