Question Detail
Question
Consider the following Python code snippet:
What is the outcome when this code is executed?
num = 10
while num > 0:
print(num)
num = num + 1What is the outcome when this code is executed?
Correct Answer
Option C is the correct answer.
Detailed Explanation
The initial
num is 10, which satisfies num > 0. Inside the loop, num is incremented (num = num + 1). This means num will always be greater than 0 (10, 11, 12, ...), so the condition num > 0 will always be True, leading to an infinite loop.
Hint
Pay close attention to how the
num variable is updated inside the while loop. Does it ever make the condition num > 0 false?