While and For Loops
This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “While and For Loops”.
1. What is the output of the following?
a) [‘ab’, ‘cd’]
b) [‘AB’, ‘CD’]
c) [None, None]
d) none of the mentioned
View Answer
a) [‘AB’, ‘CD’]
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’]
c) [‘ab’, ‘cd’]
d) none of the mentioned
View Answer
a) 1 2
b) 1 2 3
c) error
d) none of the mentioned
View Answer
1. What is the output of the following?
x = ['ab', 'cd'] for i in x: i.upper() print(x)
b) [‘AB’, ‘CD’]
c) [None, None]
d) none of the mentioned
View Answer
Answer: a
Explanation: The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.
2. What is the output of the following?Explanation: The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.
x = ['ab', 'cd'] for i in x: x.append(i.upper()) print(x)
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’]
c) [‘ab’, ‘cd’]
d) none of the mentioned
View Answer
Answer: d
Explanation: The loop does not terminate as new elements are being added to the list in each iteration.
3. What is the output of the following?Explanation: The loop does not terminate as new elements are being added to the list in each iteration.
i = 1 while True: if i%3 == 0: break print(i) i + = 1
b) 1 2 3
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.
4. What is the output of the following?Explanation: SyntaxError, there shouldn’t be a space between + and = in +=.
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
Answer: a
Explanation: Control exits the loop when i becomes 7.
Explanation: Control exits the loop when i becomes 7.
No comments:
Post a Comment