80.9k views
1 vote
Consider the following method:

public void doSomething(int n) {
if (n > 0) {
doSomething(n/2);
StdOut.print(n);
doSomething(n/2); }
}

How many recursive calls are made by doSomething(n)?

1 Answer

4 votes

Answer:

two recursive calls are made

Step-by-step explanation:

In this piece of code two recursive calls are made by doSomething(n). This is assuming that the input (n) is greater than 0. Otherwise the function will simply end because it will completely skip over the IF statement which holds the entirety of the functions code. A recursive call is represented as the name of the function/method itself within itself. Therefore, everytime the code states doSomething(n) or in this case doSomething(n/2) it is calling itself.

User Vinod Kumar
by
3.8k points