nerdexam
Python_Institute

PCEP-30-02 · Question #343

What happens when the user runs the following code? speed = 0 while speed < 30: speed = 2 if speed > 10: continue print("", end="") else: print("**")

The correct answer is B. The program outputs one asterisk ('*') to the screen. There is an error in the question as written - the stated correct answer (B) does not match the code provided. What the code actually does: Tracing the execution with speed = 0: `` speed = 0 × 2 = 0 → 0 > 10? No → print "" speed = 0 × 2 = 0 → 0 > 10? No → print "" ... (forever)…

Question

What happens when the user runs the following code? speed = 0 while speed < 30: speed = 2 if speed > 10: continue print("", end="") else: print("**")

Options

  • AThe program enters an infinite loop.
  • BThe program outputs one asterisk ('*') to the screen.
  • CThe program outputs five asterisks ('****') to the screen.
  • DThe program outputs three asterisks ('***') to the screen.

How the community answered

(14 responses)
  • A
    7% (1)
  • B
    71% (10)
  • C
    7% (1)
  • D
    14% (2)

Explanation

There is an error in the question as written - the stated correct answer (B) does not match the code provided.

What the code actually does:

Tracing the execution with speed = 0:

speed = 0 × 2 = 0  →  0 > 10? No  →  print "*"
speed = 0 × 2 = 0  →  0 > 10? No  →  print "*"
... (forever)

Because 0 × 2 = 0 on every iteration, speed never changes, 0 < 30 is always True, and the loop never exits. The real correct answer is A (infinite loop).

Why the distractors don't apply as written:

  • B, C, D all imply the program terminates, which requires speed to eventually reach ≥ 30. That never happens when speed starts at 0 and is multiplied by 2.

What the question was likely intended to be (with a non-zero starting value, e.g. speed = 5):

Iterationspeed (after *=2)speed > 10?Action
110Noprint("*")
220Yescontinue
340-Exits loop (40 ≥ 30)
else--print("**")

That would output *** (three asterisks = D), still not B.

Memory tip for exam takers: When you see speed = 0 followed by speed *= 2, immediately flag it - multiplying zero by anything stays zero. Always trace the first iteration to check if the loop variable actually changes; if it doesn't, the answer is almost always "infinite loop."

Bottom line: Double-check the original source of this question - there is likely a typo in the initial value or the operation. As written, the answer is A, not B.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice