Question 1
2 marksThis algorithm should print exactly 5 stars.
01stars = 0
02while stars <= 5
03 print("*")
04 stars = stars + 1
05endwhileState how many stars the algorithm actually prints, and write a corrected line 02.
Model answer
It prints 6 stars.
while stars < 5
Explanation
The loop runs for stars = 0, 1, 2, 3, 4 and 5, which is six passes, because <= includes 5. With stars < 5 it runs for 0 to 4, which is exactly five passes. This off-by-one error is a very common logic error: always check the first and last values that a loop condition allows.
Mark scheme
-
1 mark
6
-
1 mark
while stars < 5
accept while stars <= 4 / while stars != 5; accept Python. Also accept changing line 01 to stars = 1 if the student clearly says line 02 stays the same, as this also prints 5 stars.