This set of Python Multiple Choice Questions & Answers (MCQs) focuses 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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 …
View Answer
Explanation: 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
View Answer
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.
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
View Answer
Explanation: 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
View Answer
Explanation: SyntaxError, True is a keyword and it’s value cannot be changed.
More MCQs on While and For Loops in Python:
- Python While and For Loops MCQ (Set 2)
- Python While and For Loops MCQ (Set 3)
- Python While and For Loops MCQ (Set 4)
- Python While and For Loops MCQ (Set 5)
- Python While and For Loops MCQ (Set 6)
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
- Practice Programming MCQs
- Apply for Python Internship