Consider the following recursive C function. void get(int n) { if (n If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main() ?
Topic-wise GATE CS PYQs with verified steps
GATE CSE 2015 Set 3 · Q39
Time
void get(int n)
{
if (n<1) return;
get(n-1);
get(n-3);
printf("%d", n);
}If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?Select one answerSingle correct
Solution
Official answer: B) 25
RecursionLet denote the total number of invocations of the function get() when get(n) is executed.
From the function definition:
For (), the function immediately returns after 1 invocation:
For , the function executes 1 call itself and invokes get(n-1) and get(n-3):
Now, we compute bottom-up for :
Thus, get() is invoked 25 times, which corresponds to option (B).
Try the question first, then check your work.
Examiner trap
Set up a recurrence relation with base cases for all . Compute values bottom-up starting from to .
This Recursion previous year question is Q39 of the GATE CSE 2015 Set 3 paper. Find more GATE CSE PYQs on Recursion in Programming and Data Structures, or browse all GATE CSE PYQs.
Free diagnostic
Take the 20-minute diagnostic: the 3 topics that cost you the most marks, and a rank range. Or sit 5 related Recursion PYQs now with no account.
An official question by email at 7 AM IST, with a link to its verified solution. Unsubscribe in one click.
Recursion has 19 GATE CSE PYQs across 15 papers from 2011 to 2026. 10 of them are 2-mark MCQs like this one, and it is the only one in GATE CSE 2015 Set 3.
How GATE tests RecursionIndependent practice explanation verified by the GateAI team. GATE is conducted by the IITs; question text follows the official paper.