*Recursion* Flashcards

1
Q

Recursive method

A

A method that calls itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Depth of recursion

A

The number of times that a method calls itself

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When can problems be solved with recursion?

A

A problem can be solved with recursion if it can be broken down into successive smaller problems that are identical to the overall problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How efficient are recursive algorithms?

A

They are usually less efficient than iterative algorithms (i.e. using a loop). This is because a method call requires several actions to be performed by the JVM such as allocating memory for parameters and local variables and storing the address of the program location where control returns after the method terminates. These actions, often known as “overhead”, are not necessary with a loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Direct recursion

A

Methods that directly call themselves

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Indirect recursion

A

A method (A) that calls another method (B) which in turn calls the first method (A). There can even be several methods involved in the recursion. For example, method A could call method B, which could call method C, which calls method A.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why would you use a recursive method if an iterative algorithm executed faster?

A

The programmer may be able to design a recursive algorithm faster.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does a recursive method work?

A

If the problem can be solved now, without recursion, then the method solves it and returns (base case). If the problem cannot be solved now, then the method reduces it to a smaller but similar problem and calls itself to solve the smaller problem. Usually a problem is reduced by making the value of one or more parameters smaller with each recursive call (i.e. counts down to zero).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are a few problems that can be solved with recursion?

A

Calculating the factorial of a number, summing a range of array elements, drawing concentric circles, or finding the greatest common divisor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What happens if you forget to code a base case?

A

When the base case is reached a recursive method stops calling itself. Without a base case a method will call itself infinitely!

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can there be more than 1 base base in a recursive method?

A

Yes! There are two in the following method to calculate the values of n numbers in a Fibonacci series: public static int fib(int n) { if (n == 0) return 0; else if (n == 1) return 1; else return fib(n - 1) + fib(n - 2); }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly