Python Programming MCQ - Conditionals And Loops
8. Which of the following is a valid for loop in Python?
Explanation: For statment always ended with colon (:)
9. Which of the following sequences would be generated bt the given line of code?
range (5, 0, -2)
Explanation: Initial value is 5 which is decreased by 2 till 0 so we get 5, then 2 is decreased so we get 3 then same thing repeated we get 1 and now when 2 is decreased we get -1 which is less than 0 so we stop and hence we get 5 3 1.
10. A while loop in Python is used for what type of iteration?
Explanation: A while loop implements indefinite iteration, where the number of times the loop will be executed is not specified explicitly in advance.
11. When does the else statement written after loop executes?
Explanation: Else statment after loop will be executed only when loop condition become false.
12. What will be the output of the following code?
x = "abcdef"
i = "i"
while i in x:
print(i, end=" ")
Explanation: There will be no output since there is no i in the string x.
13. What will be the output of the following code?
x = 'abcd'
for i in range(len(x)):
Explanation: len(x) will give 4 and loop will run for 4 times starting from 0. Hence output will be 0 1 2 3.
14. What will be the output of the following code?
x = 12
for i in x:
Explanation: Objects of type int are not iterable.
Also check :