Python Questions and Answers – While and For Loops – 6
This set of Python Technical Questions & Answers focuses on “While/For Loops”.
1. What is the output of the following?
a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
View Answer
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
View Answer
Answer: c
Explanation: The else part is executed if control doesn’t break out of the loop.
Explanation: The else part is executed if control doesn’t break out of the loop.
2. What is the output of the following?
a) 0 1 2 3 4 Here
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
View Answer
b) 0 1 2 3 4 5 Here
c) 0 1 2 3 4
d) 1 2 3 4 5
View Answer
Answer: a
Explanation: The else part is executed if control doesn’t break out of the loop.
Explanation: The else part is executed if control doesn’t break out of the loop.
3. What is the output of the following?
a) 0 1 2
b) error
c) 0 1 2 0 1 2
d) none of the mentioned
View Answer
b) error
c) 0 1 2 0 1 2
d) none of the mentioned
View Answer
Answer: a
Explanation: The first statement creates a generator object.
Explanation: The first statement creates a generator object.
4. What is the output of the following?
advertisements
b) error
c) 0 1 2 0 1 2
d) none of the mentioned
View Answer
Answer: a
Explanation: We can loop over a generator object only once.
Explanation: We can loop over a generator object only once.
5. What is the output of the following?
a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
Answer: a
Explanation: Variable i takes the value of one character at a time.
Explanation: Variable i takes the value of one character at a time.
6. What is the output of the following?
a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
Answer: c
Explanation: Variable i takes the value of one word at a time.
Explanation: Variable i takes the value of one word at a time.
7. What is the output of the following?
a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
Answer: b
Explanation: The value of a[-1] changes in each iteration.
Explanation: The value of a[-1] changes in each iteration.
advertisements
a) 0 1 2 3
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
b) 0 1 2 2
c) 3 3 3 3
d) error
View Answer
Answer: a
Explanation: The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.
Explanation: The value of a[0] changes in each iteration. Since the first value that it takes is itself, there is no visible error in the current example.
9. What is the output of the following?
a) -2 -1
b) 0
c) error
d) none of the mentioned
View Answer
b) 0
c) error
d) none of the mentioned
View Answer
Answer: c
Explanation: SyntaxError, not in isn’t allowed in for loops.
Explanation: SyntaxError, not in isn’t allowed in for loops.
10. What is the output of the following?
a) m, y, , n, a, m, e, , i, s, , x,
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
b) m, y, , n, a, m, e, , i, s, , x
c) my, name, is, x,
d) error
View Answer
Answer: a
Explanation: Variable i takes the value of one character at a time.
Explanation: Variable i takes the value of one character at a time.
No comments:
Post a Comment