GATE DA 2025 Q63: Python recursion: value of f(15, 10)
- Programming, Data Structures and Algorithms
- 2 marks
- NAT
- No negative marking
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 . Note that , 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
Source: official GATE 2025 DA question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2025 · Q18Hashing with linear probing: h(x) = 3x mod 10
- 2025 · Q23Python lists: which call gives A = [1, 2, 3, 4, 5, 6]?
- 2025 · Q27When does binary search run in O(log n)?
- 2025 · Q29Insertion sort with exactly two swaps: possible values of x
- 2025 · Q47Python sets in a while loop: which set ends up with "this"?
- 2025 · Q58Shortest paths: which edges cannot be in G?