PCEP-30-02 · Question #197
What is the expected output of the following code? data = {} def func(d, key, value): d[key] = value print(func(data, '1', 'Peter'))
The correct answer is C. None. func modifies the dictionary in place but has no return statement - so it implicitly returns None. Since print() prints the return value of func(...), not what the function does internally, the output is None. Why each distractor fails: A (1) - '1' is the key argument passed…
Question
Options
- A1
- BPeter
- CNone
- Dvalue
- EThe code is erroneous.
How the community answered
(16 responses)- A13% (2)
- B6% (1)
- C81% (13)
Explanation
func modifies the dictionary in place but has no return statement - so it implicitly returns None. Since print() prints the return value of func(...), not what the function does internally, the output is None.
Why each distractor fails:
- A (1) -
'1'is the key argument passed in, never returned or printed directly. - B (Peter) -
'Peter'gets stored in the dict (data['1'] = 'Peter'), but that assignment isn't returned; it's a side effect. - D (value) -
valueis just a parameter name in the function signature, not a meaningful output. - E (erroneous) - the code runs without error;
datais successfully mutated, making this a common trap for those who confuse "works" with "returns something useful."
Memory tip: In Python, every function returns None unless you explicitly write return <something>. When you see print(some_function(...)), ask yourself: "does that function have a return statement?" If not, you're always printing None.
Community Discussion
No community discussion yet for this question.