Recursion (Module 4) Flashcards

1
Q

What is a programming technique where a method calls itself to solve a problem, and will continue to call itself until a certain condition (base case) is met?

A

recursion

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

How does recursion work?

A

breaking down a problem into smaller sub-problems, then solving each sub-problem by recursively calling the method

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

In recursion, what is used to keep track of method invocations?

A

stack

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

What is a factorial?

A

the product of a number, n, and every number below it (6! = 6 * 5 * 4 * 3 * 2 *1)

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

Let’s say that we call factorial(6). What happens to it? Then what happens if we call factorial(5)?

A
  • factorial(6) is pushed to the call stack. When factorial(5) is called, it’s pushed to the top of the call stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What must every recursive program include a ________ case.

A

base (stopping)

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

What is a condition under which a method stops making recursive calls?

A

base (stopping case)

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

What happens when a a stopping case is absent?

A

infinite recursion

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

What happens when infinite recursion occurs?

A

it leads to runtime errors and a stack overflow

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

What formulas define a sequence in terms of previous terms in the sequence?

A

recursive formulas

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

What formulas define a sequence directly, without referencing previous terms?

A

iterative formulas

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

What are the advantages of recursion?

A
  • it makes code shorter and cleaner
  • required in data structures and advanced algorithms
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the disadvantages of recursion?

A
  • it takes a lot of stack space (memory)
  • takes more processing time
  • more difficult to debug
How well did you know this?
1
Not at all
2
3
4
5
Perfectly