nerdexam
Python_Institute

PCEP-30-02 · Question #226

What is the output of the following snippet? ``python def fun(inp=2, out=3): return inp * out print(fun(out=2)) ``

The correct answer is C. 4. Calling fun(out=2) uses the keyword argument to override only out, leaving inp at its default value of 2, so the function computes 2 2 = 4, making C correct. A (2) is wrong because neither parameter equals 1 - there's no way to get a product of 2 from these values in this call…

Question

What is the output of the following snippet?
def fun(inp=2, out=3):
 return inp * out

print(fun(out=2))

Options

  • A2
  • BThe snippet is erroneous
  • C4
  • D6

How the community answered

(39 responses)
  • A
    5% (2)
  • B
    3% (1)
  • C
    82% (32)
  • D
    10% (4)

Explanation

Calling fun(out=2) uses the keyword argument to override only out, leaving inp at its default value of 2, so the function computes 2 * 2 = 4, making C correct.

A (2) is wrong because neither parameter equals 1 - there's no way to get a product of 2 from these values in this call. B is wrong because the code is perfectly valid Python; keyword arguments can be passed in any order and can override any subset of defaults. D (6) is the trap: that's what fun() returns with no arguments (2 * 3 = 6), but here out is explicitly changed to 2.

Memory tip: When you see a keyword argument call, mentally "apply the override, freeze the rest" - only the named parameter changes; every unnamed parameter stays at its default.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice