PCEP-30-02 · Question #183
The following snippet: def func(a, b): return a ** a print(func(2))
The correct answer is C. is erroneous. Option C is correct because func is defined with two parameters (a and b), but is called with only one argument (func(2)). Python raises a TypeError at runtime: "func() missing 1 required positional argument: 'b'", so the program never executes at all. Why the distractors fail…
Question
Options
- Awill output 4
- Bwill output 2
- Cis erroneous
- Dwill return None
How the community answered
(26 responses)- A8% (2)
- B12% (3)
- C77% (20)
- D4% (1)
Explanation
Option C is correct because func is defined with two parameters (a and b), but is called with only one argument (func(2)). Python raises a TypeError at runtime: "func() missing 1 required positional argument: 'b'", so the program never executes at all.
Why the distractors fail:
- A (output 4) and B (output 2) both assume the function runs successfully - it doesn't, because the missing argument causes a crash before the
returnline is ever reached. (If it did run witha=2,a ** awould be2 ** 2 = 4, but thebparameter being unused is a red herring.) - D (return None) would apply if the function had no
returnstatement - but that's irrelevant here since execution never gets that far.
Memory tip: Always count the commas. If a function signature has one comma (def func(a, b)), it needs two arguments at the call site - one more than the number of commas.
Community Discussion
No community discussion yet for this question.