nerdexam
Python_Institute

PCEP-30-02 · Question #206

Which of the following function headers is correct?

The correct answer is A. def func(a=1, b=1, c=2). Option A is correct because all parameters with default values (a=1, b=1, c=2) appear together, which is valid Python - default arguments can all follow each other in any order. Options B, C, and D all violate Python's rule that non-default parameters cannot follow default…

Question

Which of the following function headers is correct?

Options

  • Adef func(a=1, b=1, c=2):
  • Bdef func(a=1, b=1, c=2, d):
  • Cdef func(a=1, b):
  • Ddef func(a=1, b, c=2):

How the community answered

(26 responses)
  • A
    81% (21)
  • B
    12% (3)
  • C
    4% (1)
  • D
    4% (1)

Explanation

Option A is correct because all parameters with default values (a=1, b=1, c=2) appear together, which is valid Python - default arguments can all follow each other in any order. Options B, C, and D all violate Python's rule that non-default parameters cannot follow default parameters: in each case, a parameter without a default (d or b) appears after one with a default, which causes a SyntaxError. Python enforces this because if defaults could be followed by non-defaults, the interpreter couldn't determine which arguments the caller intended to pass positionally.

Memory tip: Think of it as a one-way door - once you open a default value, every parameter after it must also have a default. Any "bare" parameter (no =) must come before the first defaulted one.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice