GATE DA 2025, Q.63: Output of a recursive doubling function
The question
What does the program print?
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))Watch it solved
Solve it step by step
- a = 15 (odd). Returns 2·f(7.0, 10).
- Keep halving. f(7) = 2·f(3), f(3) = 2·f(1), f(1) = 2·f(0).
- Base. f(0, 10) = 10, so f(1) = 20, f(3) = 40, f(7) = 80.
- Answer. f(15, 10) = 160. (The values become floats such as 7.0, but the arithmetic is the same.)
Answer: 160
Official IIT Roorkee answer key: 160.
Common mistake. Following the even branch (b + f(a − 1, b)) by mistake; 15, 7, 3 and 1 are all odd.
Remember. Trace recursion on paper: write each call and its return value.
Want every question solved? The past-paper pack has all 195 questions from 2024, 2025 and 2026 for ₹499. The cheat sheet pack has 23 printable sheets for ₹399.
See the past-paper pack See the cheat sheetsSource: official GATE DA 2025 question paper and answer key (IIT Roorkee). The question is lightly reworded; every number and option is kept.