Python_Institute
PCEP-30-02 · Question #185
What is the expected output of the following code? def fun(): return 3 def add(n): return fun() + n print(add(3))
The correct answer is D. 6. D is correct because add(3) evaluates fun() + n, where fun() always returns the literal 3 and n is the argument 3, making the expression 3 + 3 = 6. Why the distractors fail: A (9): Confuses addition with multiplication - 3 × 3 = 9, but the + operator is used, not `. B (error)…
Question
What is the expected output of the following code?
def fun():
return 3
def add(n):
return fun() + n
print(add(3))
Options
- A9
- BThe program will cause an error.
- C3
- D6
How the community answered
(38 responses)- A13% (5)
- B8% (3)
- C3% (1)
- D76% (29)
Explanation
D is correct because add(3) evaluates fun() + n, where fun() always returns the literal 3 and n is the argument 3, making the expression 3 + 3 = 6.
Why the distractors fail:
- A (9): Confuses addition with multiplication -
3 × 3 = 9, but the+operator is used, not*. - B (error): Both functions are defined before they're called, and
fun()requires no arguments, so Python has no complaint. - C (3): Would be the result if
addreturned onlyfun()and ignoredn, but the+ nis always included.
Memory tip: When a function calls another function, substitute the called function's return value first, then finish the math - treat fun() like a variable that holds 3, so fun() + n becomes 3 + 3.
Community Discussion
No community discussion yet for this question.