Skip to content
GATEiit

GATE DA 2025 Q63: Python recursion: value of f(15, 10)

Question

Consider the following Python code snippet.

def f(a,b):
    if (a==0):
        return b
    if (a%2==1):
        return 2*f((a-1)/2,b)
    return b+f(a-1,b)
print(f(15,10))

The value printed by the code snippet is ____ (Answer in integer)

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 160 (official GATE 2025 final answer key)

Concept used: Tracing recursion, including Python 3's true division (/ returns a float).

First step: Trace the calls: 15 is odd, so f(15,10)=2f(7.0,10)f(15, 10) = 2 f(7.0, 10). Note that 7.0 % 2=1.07.0 \,\%\, 2 = 1.0, which still equals 1, so keep following the odd branch.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2025 DA question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key