Recursion Flashcards

1
Q

What is recursion in programming?

A

A method that calls itself to solve a smaller instance of the same problem.

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

Write the base case for calculating factorial using recursion.

A

if (n == 0) return 1;

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

What is a stack overflow error in recursion?

A

It occurs when too many recursive calls exceed the stack’s capacity.

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

How do you calculate Fibonacci numbers recursively?

A

int fibonacci(int n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}

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

Why are base cases essential in recursion?

A

To prevent infinite recursion.

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

What is tail recursion?

A

A recursion where the recursive call is the last operation in the function.

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

How do you convert a recursive algorithm into an iterative one?

A

Use loops to simulate the recursive behavior.

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

What is a recursive tree?

A

A visual representation of function calls in recursion.

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

How does recursion differ from iteration?

A

Recursion uses function calls, while iteration uses loops.

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

What does the return statement do in recursion?

A

It passes the result back up the recursive call stack.

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