What is the output of a Python while loop decrementing a counter?
Refer to the exhibit. What is output by this code? - 
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
The exam tests whether candidates can trace a while-loop that prints a value before decrementing it; the common trap is assuming the loop prints the decremented value or includes the boundary value where the condition becomes false.
This question tests understanding of Python while-loop control flow, specifically how a decrementing counter interacts with a loop condition. The community unanimously agrees the output is 8 7 6 5 because the loop prints the counter before decrementing and exits when the condition becomes false.
Many candidates choose C (4 5 6 7) because they mistakenly believe the loop prints the value after decrementing or that the boundary value 4 is included, forgetting that the condition is evaluated before each iteration and the print occurs before the decrement.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The variablecount is initialized to 8 and the loop condition is count > 4. Each iteration prints the current value of count and then decrements it by 1. The loop executes for count values 8, 7, 6, and 5; when count reaches 4 the condition 4 > 4 evaluates to False and the loop terminates. Therefore the printed output is 8 7 6 5, matching option B.Why the Other Options Are Wrong
Option A (-1 -2 -3 -4) and option D (-4 -5 -6 -7) imply the loop runs into negative numbers, which never happens because the conditioncount > 4 stops execution once count reaches 4. Option C (4 5 6 7) would only be correct if the print statement appeared after the decrement or if the condition were count >= 4; neither is the case in the code shown.Community Comment Notes
Comment [1] clearly traces each iteration, confirming that the condition is checked before the body executes and that the loop exits when count equals 4. Comment [2] reinforces the same logic, noting that the print happens before the decrement. All voters selected B, indicating strong community consensus on the correct trace.Official Reference
Exam Strategy
When tracing a loop, write down the variable's value at the start of each iteration and check the condition before executing the body. Pay close attention to whether the print or update statement comes first, as this determines whether the boundary value is included in the output.
Related Analysis
Practice All 350-401 Questions
Access 218 questions with complete answers and detailed explanations.
View Full 350-401 Practice Test →