uni 10 Flashcards
What is recursion?
A programming technique where a method calls itself.
What are the two required parts of a recursive method?
A base case and a recursive call.
What is the base case in recursion?
The condition under which the recursion stops.
What is a recursive call?
A method call where the method calls itself.
What happens if a recursive method has no base case?
It results in infinite recursion (and usually a StackOverflowError).
What is stack overflow?
An error that occurs when too many recursive calls build up without returning.
How is recursion similar to iteration?
Both repeat steps; recursion uses method calls
When is recursion preferred over iteration?
When the problem is naturally recursive (like trees
What is the recursive case?
The part of the method that calls itself to solve a smaller problem.
What is factorial recursion?
A function where n! = n * (n - 1)!
Write a factorial recursive method.
int fact(int n) { if (n == 0) return 1; else return n * fact(n - 1); }
What is linear recursion?
Each call makes at most one recursive call.
What is binary recursion?
Each call makes two recursive calls (e.g.
What is tail recursion?
Recursive call is the last thing executed by the method.
What is the base case in factorial recursion?
When n == 0 or n == 1
Is recursion tested on the AP CSA exam?
Yes
Can recursion be used to solve searching problems?
Yes
What is a recursive Fibonacci implementation?
Each call returns fib(n-1) + fib(n-2)
Is recursion efficient for Fibonacci?
No
How do you trace recursive calls?
Draw a call stack or tree showing each call and return value.
What is the biggest risk in writing recursion?
Forgetting the base case or not reducing the problem size.
What is the advantage of recursion?
Code can be simpler for certain problems like backtracking or tree traversal.
What types of problems are suited to recursion?
Divide and conquer
Can all recursion be rewritten using loops?
Yes