PCEP-30-02 · Question #154
What is the expected output of the following code? `` num = 2 + 3 * 5 print(Num) ``
The correct answer is C. The code is erroneous. Option C is correct because Python is case-sensitive - the variable is assigned as num (lowercase), but print(Num) references Num (uppercase N), which was never defined. This raises a NameError at runtime before any output is produced. Why the distractors are wrong: D (17)…
Question
num = 2 + 3 * 5
print(Num)
Options
- A25
- B17.0
- CThe code is erroneous.
- D17
How the community answered
(25 responses)- A4% (1)
- B4% (1)
- C80% (20)
- D12% (3)
Explanation
Option C is correct because Python is case-sensitive - the variable is assigned as num (lowercase), but print(Num) references Num (uppercase N), which was never defined. This raises a NameError at runtime before any output is produced.
Why the distractors are wrong:
- D (17) would be the arithmetic result if the variable name were consistent, since Python follows standard operator precedence (multiplication before addition:
3 × 5 = 15, then2 + 15 = 17) - but the code never reachesprintsuccessfully. - A (25) incorrectly applies left-to-right evaluation
(2 + 3) × 5 = 25, ignoring that*has higher precedence than+. - B (17.0) makes the same precedence error as D and also wrongly assumes the result is a float - all operands are integers, so the result would be
int, notfloat.
Memory tip: Think "Python reads variable names like a strict teacher - num and Num are completely different names. Always check that the variable you assign and the one you use match exactly in case."
Community Discussion
No community discussion yet for this question.