nerdexam
Python_Institute

PCEP-30-02 · Question #195

Which of the following lines properly starts a function using two parameters, both with zeroed default values?

The correct answer is A. def fun(a=0, b=0). Option A is the only choice that assigns a default value of 0 to both parameters, satisfying the requirement that each parameter is "zeroed" by default. Option B is actually a SyntaxError in Python - a parameter without a default (b) cannot follow one that has a default (a=0)…

Question

Which of the following lines properly starts a function using two parameters, both with zeroed default values?

Options

  • Adef fun(a=0, b=0):
  • Bdef fun(a=0, b):
  • Cdef fun(a, b=0):
  • Ddef fun(a, b=0):

How the community answered

(40 responses)
  • A
    78% (31)
  • B
    13% (5)
  • C
    8% (3)
  • D
    3% (1)

Explanation

Option A is the only choice that assigns a default value of 0 to both parameters, satisfying the requirement that each parameter is "zeroed" by default. Option B is actually a SyntaxError in Python - a parameter without a default (b) cannot follow one that has a default (a=0), because Python wouldn't know which argument maps where. Options C and D are identical and only give a default to the second parameter (b=0), leaving a without any default - so a caller would be forced to supply a explicitly, meaning both parameters are not zeroed.

Memory tip: Think of it as a sliding rule - once you start assigning defaults, every parameter to the right must also have a default. If you want all defaults, every parameter needs one, top to bottom.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice