Chapter 12 Flashcards

(19 cards)

1
Q

What is recursion in programming?

A

Recursion is a technique where a method calls itself to solve a smaller instance of the same problem, with a base case to stop the calls.

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

What is a recursive definition?

A

It’s a definition that uses itself in the explanation, like defining a list as a number or a number followed by another list.

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

What is the base case and recursive case in recursion?

A

Base case stops recursion with a known answer. Recursive case reduces the problem and calls the function again.

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

How do you think recursively?

A

Break the problem into smaller instances of the same problem until you reach a trivial case.

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

How is sum from 1 to n computed recursively?

A

sum(n) = n + sum(n-1), with base case sum(1) = 1.

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

Can a recursive function using if-else?

A

Yes, it uses conditionals. An if-statement checks the base case and returns; the else is implied by code following the if.

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

How does recursion compare to iteration?

A

Iteration builds from bottom up using loops; recursion breaks down the problem top-down with stack frames.

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

What is the call stack in recursion?

A

Each function call adds a new stack frame. The program unwinds them once a base case is reached.

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

What causes a stack overflow in recursion?

A

Infinite recursion without a base case leads to memory overflow due to unending stack frames.

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

How does recursion solve Towers of Hanoi?

A

Move n-1 disks to auxiliary peg, move largest to destination, then move n-1 disks from auxiliary to destination.

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

How is factorial defined recursively?

A

n! = n * (n-1)!, with base case 1! = 1.

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

When is recursion preferred over iteration?

A

When the problem has a naturally recursive structure like trees or divide-and-conquer algorithms.

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

What are flowcharts and pseudocode used for?

A

To plan algorithms with diagrams and plain English before writing actual code.

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

How can a list like [24, 88, 40, 37] be defined recursively?

A

By expressing it as a number followed by a comma and another list, repeatedly, until the last element becomes the base case.

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

Why might recursion be less efficient than iteration?

A

Each recursive call adds a new stack frame, increasing memory usage and method call overhead.

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

When should you avoid recursion?

A

Avoid recursion when an iterative approach is clearer, faster, or when the recursion doesn’t simplify the problem.

17
Q

What is recursive tracing?

A

It means tracking each recursive call and return to understand how inputs shrink and outputs build back up.

18
Q

What are the parts of a recursive Java method?

A

A base case that ends recursion and a recursive case that reduces the input and calls the method again.

19
Q

How does recursion differ from iteration in memory usage?

A

Recursion consumes stack space for each call; iteration typically uses a single frame.