Recursion - AP Computer Science A
Card 1 of 30
What is the term for the part of a recursive function where it calls itself?
What is the term for the part of a recursive function where it calls itself?
Tap to reveal answer
Recursive call. The self-referential part of recursive functions.
Recursive call. The self-referential part of recursive functions.
← Didn't Know|Knew It →
Which option best describes a recursive backtracking approach?
Which option best describes a recursive backtracking approach?
Tap to reveal answer
Trying all possibilities via recursion to solve a problem. Systematic exploration with backtracking on failure.
Trying all possibilities via recursion to solve a problem. Systematic exploration with backtracking on failure.
← Didn't Know|Knew It →
What is the typical reason for using recursion over iteration?
What is the typical reason for using recursion over iteration?
Tap to reveal answer
Recursion can be more intuitive for problems with a recursive nature. Natural mapping to problem structure and readability.
Recursion can be more intuitive for problems with a recursive nature. Natural mapping to problem structure and readability.
← Didn't Know|Knew It →
What is a recursive helper function?
What is a recursive helper function?
Tap to reveal answer
A function used to simplify recursive calls by handling initial parameters. Auxiliary function that assists the main recursive function.
A function used to simplify recursive calls by handling initial parameters. Auxiliary function that assists the main recursive function.
← Didn't Know|Knew It →
Identify the recursive call in the function: double power(double x, int n) { return (n==0) ? 1 : x*power(x, n-1); }
Identify the recursive call in the function: double power(double x, int n) { return (n==0) ? 1 : x*power(x, n-1); }
Tap to reveal answer
Recursive call: power(x, n-1);. Exponentiation using recursive multiplication.
Recursive call: power(x, n-1);. Exponentiation using recursive multiplication.
← Didn't Know|Knew It →
What is recursion in computer science?
What is recursion in computer science?
Tap to reveal answer
Recursion is a method where a function calls itself. Self-referential programming technique for solving complex problems.
Recursion is a method where a function calls itself. Self-referential programming technique for solving complex problems.
← Didn't Know|Knew It →
Identify the base case in the function: int fact(int n) { if (n==0) return 1; else return n*fact(n-1); }
Identify the base case in the function: int fact(int n) { if (n==0) return 1; else return n*fact(n-1); }
Tap to reveal answer
Base case: if (n==0) return 1;. The stopping condition that prevents infinite recursion.
Base case: if (n==0) return 1;. The stopping condition that prevents infinite recursion.
← Didn't Know|Knew It →
What is the purpose of a base case in recursion?
What is the purpose of a base case in recursion?
Tap to reveal answer
To stop recursion and prevent infinite loops. Essential terminating condition for recursive functions.
To stop recursion and prevent infinite loops. Essential terminating condition for recursive functions.
← Didn't Know|Knew It →
Which option best describes tail recursion?
Which option best describes tail recursion?
Tap to reveal answer
Recursive call is the last operation in the function. Optimization where recursion is the final operation performed.
Recursive call is the last operation in the function. Optimization where recursion is the final operation performed.
← Didn't Know|Knew It →
Identify the recursive call in the function: int sum(int n) { if (n<=0) return 0; else return n+sum(n-1); }
Identify the recursive call in the function: int sum(int n) { if (n<=0) return 0; else return n+sum(n-1); }
Tap to reveal answer
Recursive call: sum(n-1);. The line where the function invokes itself with modified parameters.
Recursive call: sum(n-1);. The line where the function invokes itself with modified parameters.
← Didn't Know|Knew It →
What is the main advantage of recursion?
What is the main advantage of recursion?
Tap to reveal answer
Simplifies code for problems that have recursive structure. Elegant solution for naturally hierarchical problems.
Simplifies code for problems that have recursive structure. Elegant solution for naturally hierarchical problems.
← Didn't Know|Knew It →
What is a common disadvantage of recursion?
What is a common disadvantage of recursion?
Tap to reveal answer
May lead to high memory use due to call stack. Each call adds a frame to the call stack.
May lead to high memory use due to call stack. Each call adds a frame to the call stack.
← Didn't Know|Knew It →
What is a recursive algorithm?
What is a recursive algorithm?
Tap to reveal answer
An algorithm that solves a problem by solving smaller instances of the same problem. Breaks problems down into smaller versions of themselves.
An algorithm that solves a problem by solving smaller instances of the same problem. Breaks problems down into smaller versions of themselves.
← Didn't Know|Knew It →
Identify the base case in the function: int fib(int n) { if (n<=1) return n; else return fib(n-1)+fib(n-2); }
Identify the base case in the function: int fib(int n) { if (n<=1) return n; else return fib(n-1)+fib(n-2); }
Tap to reveal answer
Base case: if (n<=1) return n;. Handles the terminating conditions for small inputs.
Base case: if (n<=1) return n;. Handles the terminating conditions for small inputs.
← Didn't Know|Knew It →
State the recursive formula for calculating Fibonacci numbers.
State the recursive formula for calculating Fibonacci numbers.
Tap to reveal answer
$F(n) = F(n-1) + F(n-2)$ with base cases $F(0)=0, F(1)=1$. Each term is the sum of the two preceding terms.
$F(n) = F(n-1) + F(n-2)$ with base cases $F(0)=0, F(1)=1$. Each term is the sum of the two preceding terms.
← Didn't Know|Knew It →
What is the difference between direct and indirect recursion?
What is the difference between direct and indirect recursion?
Tap to reveal answer
Direct: function calls itself; Indirect: function calls another that calls it. Direct calls itself; indirect calls through other functions.
Direct: function calls itself; Indirect: function calls another that calls it. Direct calls itself; indirect calls through other functions.
← Didn't Know|Knew It →
Which option correctly describes mutual recursion?
Which option correctly describes mutual recursion?
Tap to reveal answer
Two or more functions call each other. Functions form a calling cycle among themselves.
Two or more functions call each other. Functions form a calling cycle among themselves.
← Didn't Know|Knew It →
Find and correct the error: int fact(int n) { return n*fact(n-1); }
Find and correct the error: int fact(int n) { return n*fact(n-1); }
Tap to reveal answer
Add base case: if (n==0) return 1;. Missing termination condition causes infinite recursion.
Add base case: if (n==0) return 1;. Missing termination condition causes infinite recursion.
← Didn't Know|Knew It →
Identify the base case in the function: void print(int n) { if (n==0) return; else { print(n-1); System.out.println(n); } }
Identify the base case in the function: void print(int n) { if (n==0) return; else { print(n-1); System.out.println(n); } }
Tap to reveal answer
Base case: if (n==0) return;. The terminating condition that stops recursion.
Base case: if (n==0) return;. The terminating condition that stops recursion.
← Didn't Know|Knew It →
What is the purpose of using a helper method in recursion?
What is the purpose of using a helper method in recursion?
Tap to reveal answer
To manage additional parameters or initial setup. Simplifies recursive function interfaces and setup.
To manage additional parameters or initial setup. Simplifies recursive function interfaces and setup.
← Didn't Know|Knew It →
What is memoization in the context of recursion?
What is memoization in the context of recursion?
Tap to reveal answer
Caching results of expensive function calls to avoid recalculations. Dynamic programming technique to optimize recursive algorithms.
Caching results of expensive function calls to avoid recalculations. Dynamic programming technique to optimize recursive algorithms.
← Didn't Know|Knew It →
What is the primary challenge when debugging recursive functions?
What is the primary challenge when debugging recursive functions?
Tap to reveal answer
Understanding the call stack and flow of recursive calls. Complex execution flow makes tracing difficult.
Understanding the call stack and flow of recursive calls. Complex execution flow makes tracing difficult.
← Didn't Know|Knew It →
State the recursive formula for calculating factorial.
State the recursive formula for calculating factorial.
Tap to reveal answer
$n! = n \times (n-1)!$ with base case $0! = 1$. Product of all positive integers up to $n$.
$n! = n \times (n-1)!$ with base case $0! = 1$. Product of all positive integers up to $n$.
← Didn't Know|Knew It →
What does the stack frame contain in a recursive call?
What does the stack frame contain in a recursive call?
Tap to reveal answer
Function's local variables, parameters, and return address. Information stored for each recursive call.
Function's local variables, parameters, and return address. Information stored for each recursive call.
← Didn't Know|Knew It →
Identify the error in the function: int sum(int n) { return n + sum(n-1); }
Identify the error in the function: int sum(int n) { return n + sum(n-1); }
Tap to reveal answer
Missing base case. No terminating condition leads to infinite recursion.
Missing base case. No terminating condition leads to infinite recursion.
← Didn't Know|Knew It →
What is the difference between recursion and iteration?
What is the difference between recursion and iteration?
Tap to reveal answer
Recursion uses self-calling functions; iteration uses loops. Recursion uses function calls; iteration uses loops.
Recursion uses self-calling functions; iteration uses loops. Recursion uses function calls; iteration uses loops.
← Didn't Know|Knew It →
Identify the recursive call in the function: void reversePrint(int[] arr, int n) { if (n==0) return; System.out.println(arr[n-1]); reversePrint(arr, n-1); }
Identify the recursive call in the function: void reversePrint(int[] arr, int n) { if (n==0) return; System.out.println(arr[n-1]); reversePrint(arr, n-1); }
Tap to reveal answer
Recursive call: reversePrint(arr, n-1);. Self-call that processes remaining array elements.
Recursive call: reversePrint(arr, n-1);. Self-call that processes remaining array elements.
← Didn't Know|Knew It →
What is a recursive descent parser?
What is a recursive descent parser?
Tap to reveal answer
A top-down parser built from a set of mutually recursive procedures. Parsing technique using recursive grammar rules.
A top-down parser built from a set of mutually recursive procedures. Parsing technique using recursive grammar rules.
← Didn't Know|Knew It →
What is the importance of a well-defined base case in recursion?
What is the importance of a well-defined base case in recursion?
Tap to reveal answer
Prevents infinite recursion and stack overflow. Ensures recursion terminates properly.
Prevents infinite recursion and stack overflow. Ensures recursion terminates properly.
← Didn't Know|Knew It →
Which option best describes the stack overflow error in recursion?
Which option best describes the stack overflow error in recursion?
Tap to reveal answer
Occurs when recursion exceeds the stack size limit. Runtime error from excessive recursive calls.
Occurs when recursion exceeds the stack size limit. Runtime error from excessive recursive calls.
← Didn't Know|Knew It →