GATE | GATE-CS-2015 (Set 3) | Question 49
Consider the following recursive JAVA function. If get(6) function is being called in main() then how many times will the get() function be invoked before returning to the main()?
Java
static void get ( int n){
if (n < 1 )
return ;
get(n - 1 );
get(n - 3 );
System.out.print(n);
}
|
(A)
15
(B)
25
(C)
35
(D)
45
Answer: (B)
Explanation:
get(6) [25 Calls]
/ \\
[17 Calls] get(5) get(3) [7 Calls]
/ \\
get(4) get(2)[5 Calls]
/ \\
[7 Calls] get(3) get(1)[3 Calls]
/ \\
get(2) get(0)
/ \\
[3 Calls]get(1) get(-1)
/ \\
get(0) get(-2)
Hence Option (B) is the correct answer.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Last Updated :
28 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...