Python Control Flow Quiz
1. What is the output? x = 10 if x > 5: print("A") else: print("B")
A. B
B. No output
C. Error
D. A
2. What is the output? x = 15 if x < 10: print("Low") elif x < 20: print("Medium") else: print("High")
A. Low
B. B
C. Medium
D. High
3. What is the output? for i in range(3): print(i) else: print("Done")
A. 0 1 2
B. Done 0 1 2
C. 0 1 Done
D. 0 1 2 Done
4. What is the output? for i in range(5): if i == 2: break print(i)
A. 0 1 2
B. 0 1 2 3 4
C. 0 1
D. 2 3 4
5. What is the output? for i in range(5): if i == 2: continue print(i)
A. 0 1
B. 2 3 4
C. 0 1 2 3 4
D. 0 1 3 4
6. What is the output? for i in range(3): if i == 1: break else: print("Done")
A. 0 1
B. Done
C. 0 Done
D. 0
7. What is the output? x = 0 if x: print("True") else: print("False")
A. True
B. Error
C. 0
D. False
8. What is the output? for i in range(4): if i % 2 == 0: continue print(i)
A. 2 3
B. 0 2
C. 1 3
D. 0 1 2 3
9. What is the output? for i in range(3): print(i) if i == 1: break else: print("Done")
A. 1
B. 0 1
C. 0
D. 0 1 Done
10. What is the output? x = 20 if x < 10: print("A") elif x < 20: print("B") elif x == 20: print("C") else: print("D")
A. C
B. D
C. A
D. B
1 comment:
What is the output?
x = 10 if x > 5: print("A") else: print("B")
The condition x > 5 is true, so the if block executes.
What is the output?
x = 15 if x < 10: print("Low") elif x < 20: print("Medium") else: print("High")
The first false condition is followed by x < 20, which is true for 15.
What is the output?
for i in range(3): print(i) else: print("Done")
The loop completes normally, so its else clause executes after the iterations.
What is the output?
for i in range(5): if i == 2: break print(i)
Values 0 and 1 are printed before i becomes 2 and break terminates the loop.
What is the output?
for i in range(5): if i == 2: continue print(i)
continue skips the remaining statements for i = 2 and then the loop proceeds.
What is the output?
for i in range(3): if i == 1: break else: print("Done")
The loop prints no value, and break prevents the else clause from running.
What is the output?
x = 0 if x: print("True") else: print("False")
Because x is 0, the if condition is false and the else block executes.
What is the output?
for i in range(4): if i % 2 == 0: continue print(i)
Odd values do not satisfy i % 2 == 0, so they reach print(i).
What is the output?
for i in range(3): print(i) if i == 1: break else: print("Done")
The loop prints 0 and 1, then break terminates the loop before else can execute.
What is the output?
x = 20 if x < 10: print("A") elif x < 20: print("B") elif x == 20: print("C") else: print("D")
The third condition x == 20 is true, so that branch executes.
Post a Comment