What is the output of a Python while loop decrementing a counter?

Python Programming for Automation

Refer to the exhibit. What is output by this code? - image

  1. 1 -2 -3 -4
  2. 8 7 6 5 Source Reference Answer
  3. 4 5 6 7
  4. 4 -5 -6 -7

Community Votes

B
100%

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)

Mekai2020 👍 10 Selected: B
while count > 4 : 8 = True while count > 4 : 7 = True while count > 4 : 6 = True while count > 4 : 5 = True while count > 4 : 4 = False!
slacker_at_work 👍 2 Selected: B
count = 8 while count > 4: print(count) count -= 1 This will print the numbers 8, 7, 6, and 5, and then the loop will terminate because count will no longer be greater than 4.
Osama_anwar 👍 1 Selected: B
Correct

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

Expert Analysis

Why the Answer Is Correct

The variable count 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 condition count > 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 →

← Back to 350-401 Study Guide