Python Questions and Answers – While and For Loops – 2

This set of Advanced Python Questions & Answers focuses on “While and For Loops – 2”.

1. 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 0
b) 0 1 2
c) error
d) none of the mentioned
View Answer

Answer: b
Explanation: The else part is not executed if control breaks out of the loop.
advertisement
advertisement

2. 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
View Answer

Answer: b
Explanation: The else part is executed when the condition in the while statement is false.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
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
View Answer

Answer: d
Explanation: NameError, i is not defined.
advertisement

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

x = "abcdef"
i = "i"
while i in x:
    print(i, end=" ")

a) no output
b) i i i i i i …
c) a b c d e f
d) abcdef
View Answer

Answer: a
Explanation: “i” is not in “abcdef”.

5. 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
View Answer

Answer: c
Explanation: As the value of i or x isn’t changing, the condition will always evaluate to True.

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

Answer: b
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.

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) i i i i i i
b) a a a a a a
c) a a a a a
d) none of the mentioned
View Answer

Answer: b
Explanation: The string x is being shortened by one character in each iteration.

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
b) a a a a a a
c) a a a a a a …
d) a
View Answer

Answer: c
Explanation: String x is not being altered and i is in x[:-1].

9. 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 a a a a a
b) a
c) no output
d) error
View Answer

Answer: b
Explanation: The string x is being shortened by one character in each iteration.

10. 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
View Answer

Answer: c
Explanation: i is not in x[1:].

Sanfoundry Global Education & Learning Series – Python.

To practice all advanced questions on Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.