This set of Python Multiple Choice Questions & Answers (MCQs) 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
Explanation: The else part is not executed if control breaks out of the loop.
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
Explanation: The else part is executed when the condition in the while statement is false.
3. 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
View Answer
Explanation: NameError, i is not defined.
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
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
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
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
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
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
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
Explanation: i is not in x[1:].
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Programming Internship
- Check Python Books
- Check Information Technology Books
- Apply for Python Internship
- Practice Programming MCQs