nerdexam
Python_Institute

PCEP-30-02 · Question #207

``python def func(x, y): if x == y: return x else: return func(x, y-1) print(func(0, 3)) `` What is the output of the following snippet?

The correct answer is B. 0. Option B is correct because the function recursively decrements y by 1 on each call until x == y. Starting with func(0, 3), the chain goes func(0,3) → func(0,2) → func(0,1) → func(0,0), at which point x == y and the function returns x, which is 0. Why the distractors are wrong…

Question

def func(x, y):
 if x == y:
 return x
 else:
 return func(x, y-1)

print(func(0, 3))
What is the output of the following snippet?

Options

  • AThe snippet will cause a runtime error.
  • B0
  • C1
  • D2

How the community answered

(45 responses)
  • A
    16% (7)
  • B
    73% (33)
  • C
    4% (2)
  • D
    7% (3)

Explanation

Option B is correct because the function recursively decrements y by 1 on each call until x == y. Starting with func(0, 3), the chain goes func(0,3)func(0,2)func(0,1)func(0,0), at which point x == y and the function returns x, which is 0.

Why the distractors are wrong:

  • A is wrong because recursion terminates cleanly - y decrements toward x=0 and the base case is always reached.
  • C and D are wrong because the return value is x (which never changes), not the intermediate value of y at any step.

Memory tip: Read the function as "keep subtracting 1 from y until y equals x, then return x." Since x is fixed at 0 and y chases it downward, the answer is always whatever x was passed in - here, 0.

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice