Deck 13: Recursion

Full screen (f)
exit full mode
Question
The process of solving a problem by reducing it to smaller versions of itself is called recursion.
Use Space or
up arrow
down arrow
to flip the card.
Question
A program will terminate after completing any particular recursive call.
Question
In the base case of a recursive solution, the solution is obtained through a call to a smaller version of the original method.
Question
A general case to a recursive algorithm must eventually reduce to a base case.
Question
A method that calls another method and eventually results in the original method call is called indirectly recursive.
Question
The body of a recursive method contains a statement that causes the same method to execute before completing the current call.
Question
Every recursive definition can have zero or more base cases.
Question
A recursive method in which the first statement executed is a recursive call is called a tail recursive method.
Question
You can think of a recursive method as having unlimited copies of itself.
Question
Every recursive call has its own code.
Question
The general case of a recursive solution is the case for which the solution is obtained directly.
Question
To design a recursive method, you must determine the limiting conditions.
Question
The following is an example of a recursive method.public static int recFunc(int x)
{
return (nextNum(nextNum(x)));
}where nextNum is method such that nextNum(x) = x + 1.
Question
The recursive implementation of the factorial method is an example of a tail recursive method.
Question
A method is called directly recursive if it calls another recursive method.
Question
In reality, if you execute an infinite recursive method on a computer, it will execute forever.
Question
If every recursive call results in another recursive call, then the recursive method (algorithm) is said to have infinite recursion.
Question
The following is a valid recursive definition to determine the factorial of a non-negative integer.0! = 1
1! = 1
n! = n * (n - 1)! if n > 0
Question
The base case starts the recursion.
Question
A method that calls itself is an iterative method.
Question
The limiting condition for a recursive method using a list might be the number of elements in the list.
Question
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}What is the output of func2(2, 3)?

A) 2
B) 3
C) 5
D) 6
Question
public static int func1(int m, int n)
{
If (m == n || n == 1)
Return 1;
Else
Return func1(m - 1, n - 1) + n * func1(m - 1, n);
}Given the code in the accompanying figure, which of the following method calls would result in the value 1 being returned?

A) func1(1, 0)
B) func1(1, 1)
C) func1(1, 2)
D) func1(2, 0)
Question
There are two base cases in the recursive implementation of generating a Fibonacci sequence.
Question
The overhead associated with iterative methods is greater in terms of both memory space and computer time compared to the overhead associated with executing recursive methods.
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What does the code in the accompanying figure do?

A) Returns the cube of the number n
B) Returns the sum of the cubes of the numbers, 0 to n
C) Returns three times the number n
D) Returns the next number in a Fibonacci sequence
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}How many base cases are in the code in the accompanying figure?

A) zero
B) one
C) two
D) three
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the output of exampleRecursion(3)?

A) 25
B) 32
C) 36
D) 42
Question
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}Which of the following statements about the code in the accompanying is always true?

A) func2(m, n) = func2(n, m) for m >= 0
B) func2(m, n) = m * n for n >= 0
C) func2(m, n) = m + n for n >= 0
D) func2(m, n) = n * m for m >= 0
Question
Consider the following definition of a recursive method.public static int recFunc(int num)
{
If (num >= 10)
Return 10;
Else
Return num * recFunc(num + 1);
}What is the output of the following statement?System.out.println(recFunc(8));

A) 8
B) 72
C) 720
D) None of these
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}Which of the following is an invalid call to the method in the accompanying figure?

A) exampleRecursion(-1)
B) exampleRecursion( 0)
C) exampleRecursion(1)
D) exampleRecursion(999)
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the output of exampleRecursion(0)?

A) 0
B) 3
C) 9
D) 27
Question
Which of the following statements is NOT true about infinite recursion?

A) In theory, infinite recursion executes forever.
B) In reality, infinite recursion will lead to a computer running out of memory and abnormal termination of the program.
C) Methods that are indirectly recursive always lead to infinite recursion.
D) If recursive calls never lead to a base case, infinite recursion occurs.
Question
A recursive solution is always a better alternative to an iterative solution.
Question
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}What is the limiting condition of the code in the accompanying figure?

A) n >= 0
B) m > n
C) m >= 0
D) n > m
Question
Which of the following statements describe the base case of a recursive algorithm?
(i) F(0) = 0;
(ii) F(x) = 2 * F(x - 1);
(iii) if (x == 0)
F(x) = 5 + x;

A) Only (i)
B) Only (ii)
C) Only (iii)
D) Both (i) and (iii)
Question
Recursive algorithms are implemented using while loops.
Question
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the limiting condition of the code in the accompanying figure?

A) n >= 0
B) n > 0
C) n > 1
D) n >= 1
Question
Consider the following definition of a recursive method. public static int foo(int n) //Line 1
{ //Line 2
If (n == 0) //Line 3
Return 0; //Line 4
Else //Line 5
Return n + foo(n - 1); //Line 6
}Which of the statements represent the base case?

A) Statements in Lines 3 and 4
B) Statements in Lines 5 and 6
C) Statements in Lines 3, 4, and 5
D) None of these
Question
public static int func1(int m, int n)
{
If (m == n || n == 1)
Return 1;
Else
Return func1(m - 1, n - 1) + n * func1(m - 1, n);
}What precondition must exist in order to prevent the code in the accompanying figure from infinite recursion?

A) m >= 0 and n >= 0
B) m >= 0 and n >= 1
C) m >= 1 and n >= 0
D) m >= 1 and n >= 1
Question
Assume there are four methods A, B, C, and D. If method A calls method B, method B calls method C, method C calls method D, and method D calls method A, which of the following methods is indirectly recursive?

A) A
B) B
C) C
D) D
Question
Consider the following definition of a recursive method.public static int mystery(int[] list, int first, int last)
{
If (first == last)
Return list[first];
Else
Return list[first] + mystery(list, first + 1, last);
}Given the declarationint[] alpha = {1, 4, 5, 8, 9};What is the output of the following statement?System.out.println(mystery(alpha, 0, 4));

A) 1
B) 18
C) 27
D) 32
Question
In the recursive algorithm for the nth Fibonacci number, there are ____ base case(s).

A) zero
B) one
C) two
D) three
Question
Consider the following definition of a recursive method.public static int recFunc(int num)
{
If (num >= 10)
Return 10;
Else
Return num * recFunc(num + 1);
}What is the output of the following statement?System.out.println(recFunc(10));

A) 10
B) 110
C) This statement results in infinite recursion.
D) None of these
Question
Consider the following definition of a recursive method.public static int strange(int[] list, int first, int last)
{
If (first == last)
Return list[first];
Else
Return list[first] + strange(list, first + 1, last);
}Given the declarationint[] beta = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25};What is the output of the following statement?System.out.println(strange(beta, 4, 7));

A) 27
B) 33
C) 55
D) 66
Question
____ is NOT an iterative control structure.

A) A while loop
B) A for loop
C) Recursion
D) A do...while loop
Question
If you are building a mission control system, ____.

A) you should always use iteration
B) you should always use recursion
C) use the most efficient solution
D) use the solution that makes the most intuitive sense
Question
What is the first step in the Tower of Hanoi recursive algorithm?

A) Move the top n disks from needle 1 to needle 2, using needle 3 as the intermediate needle.
B) Move disk number n from needle 1 to needle 3.
C) Move the top n - 1 disks from needle 2 to needle 3, using needle 1 as the intermediate needle.
D) Move the top n - 1 disks from needle 1 to needle 2, using needle 3 as the intermediate needle.
Question
Using a recursive algorithm to solve the Tower of Hanoi problem, a computer that can generate one billion moves per second would take ____ years to solve the problem.

A) 39
B) 400
C) 500
D) 10,000
Question
The third Fibonacci number is ____.

A) the sum of the first two Fibonacci numbers
B) the sum of the last two Fibonacci numbers
C) the product of the first two Fibonacci numbers
D) the product of the last two Fibonacci numbers
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 13: Recursion
1
The process of solving a problem by reducing it to smaller versions of itself is called recursion.
True
2
A program will terminate after completing any particular recursive call.
False
3
In the base case of a recursive solution, the solution is obtained through a call to a smaller version of the original method.
False
4
A general case to a recursive algorithm must eventually reduce to a base case.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
A method that calls another method and eventually results in the original method call is called indirectly recursive.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
6
The body of a recursive method contains a statement that causes the same method to execute before completing the current call.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
7
Every recursive definition can have zero or more base cases.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
8
A recursive method in which the first statement executed is a recursive call is called a tail recursive method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
9
You can think of a recursive method as having unlimited copies of itself.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
10
Every recursive call has its own code.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
11
The general case of a recursive solution is the case for which the solution is obtained directly.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
To design a recursive method, you must determine the limiting conditions.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
13
The following is an example of a recursive method.public static int recFunc(int x)
{
return (nextNum(nextNum(x)));
}where nextNum is method such that nextNum(x) = x + 1.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
14
The recursive implementation of the factorial method is an example of a tail recursive method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
A method is called directly recursive if it calls another recursive method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
16
In reality, if you execute an infinite recursive method on a computer, it will execute forever.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
17
If every recursive call results in another recursive call, then the recursive method (algorithm) is said to have infinite recursion.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
18
The following is a valid recursive definition to determine the factorial of a non-negative integer.0! = 1
1! = 1
n! = n * (n - 1)! if n > 0
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
19
The base case starts the recursion.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
20
A method that calls itself is an iterative method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
21
The limiting condition for a recursive method using a list might be the number of elements in the list.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
22
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}What is the output of func2(2, 3)?

A) 2
B) 3
C) 5
D) 6
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
23
public static int func1(int m, int n)
{
If (m == n || n == 1)
Return 1;
Else
Return func1(m - 1, n - 1) + n * func1(m - 1, n);
}Given the code in the accompanying figure, which of the following method calls would result in the value 1 being returned?

A) func1(1, 0)
B) func1(1, 1)
C) func1(1, 2)
D) func1(2, 0)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
There are two base cases in the recursive implementation of generating a Fibonacci sequence.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
The overhead associated with iterative methods is greater in terms of both memory space and computer time compared to the overhead associated with executing recursive methods.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What does the code in the accompanying figure do?

A) Returns the cube of the number n
B) Returns the sum of the cubes of the numbers, 0 to n
C) Returns three times the number n
D) Returns the next number in a Fibonacci sequence
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}How many base cases are in the code in the accompanying figure?

A) zero
B) one
C) two
D) three
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the output of exampleRecursion(3)?

A) 25
B) 32
C) 36
D) 42
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}Which of the following statements about the code in the accompanying is always true?

A) func2(m, n) = func2(n, m) for m >= 0
B) func2(m, n) = m * n for n >= 0
C) func2(m, n) = m + n for n >= 0
D) func2(m, n) = n * m for m >= 0
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
Consider the following definition of a recursive method.public static int recFunc(int num)
{
If (num >= 10)
Return 10;
Else
Return num * recFunc(num + 1);
}What is the output of the following statement?System.out.println(recFunc(8));

A) 8
B) 72
C) 720
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}Which of the following is an invalid call to the method in the accompanying figure?

A) exampleRecursion(-1)
B) exampleRecursion( 0)
C) exampleRecursion(1)
D) exampleRecursion(999)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
32
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the output of exampleRecursion(0)?

A) 0
B) 3
C) 9
D) 27
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
33
Which of the following statements is NOT true about infinite recursion?

A) In theory, infinite recursion executes forever.
B) In reality, infinite recursion will lead to a computer running out of memory and abnormal termination of the program.
C) Methods that are indirectly recursive always lead to infinite recursion.
D) If recursive calls never lead to a base case, infinite recursion occurs.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
A recursive solution is always a better alternative to an iterative solution.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
public static int func2(int m, int n)
{
If (n == 0)
Return 0;
Else
Return m + func2(m, n - 1);
}What is the limiting condition of the code in the accompanying figure?

A) n >= 0
B) m > n
C) m >= 0
D) n > m
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
Which of the following statements describe the base case of a recursive algorithm?
(i) F(0) = 0;
(ii) F(x) = 2 * F(x - 1);
(iii) if (x == 0)
F(x) = 5 + x;

A) Only (i)
B) Only (ii)
C) Only (iii)
D) Both (i) and (iii)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
Recursive algorithms are implemented using while loops.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
38
public static int exampleRecursion (int n)
{
If (n == 0)
Return 0;
Else
Return exampleRecursion(n - 1) + n * n * n;
}What is the limiting condition of the code in the accompanying figure?

A) n >= 0
B) n > 0
C) n > 1
D) n >= 1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
39
Consider the following definition of a recursive method. public static int foo(int n) //Line 1
{ //Line 2
If (n == 0) //Line 3
Return 0; //Line 4
Else //Line 5
Return n + foo(n - 1); //Line 6
}Which of the statements represent the base case?

A) Statements in Lines 3 and 4
B) Statements in Lines 5 and 6
C) Statements in Lines 3, 4, and 5
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
public static int func1(int m, int n)
{
If (m == n || n == 1)
Return 1;
Else
Return func1(m - 1, n - 1) + n * func1(m - 1, n);
}What precondition must exist in order to prevent the code in the accompanying figure from infinite recursion?

A) m >= 0 and n >= 0
B) m >= 0 and n >= 1
C) m >= 1 and n >= 0
D) m >= 1 and n >= 1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
41
Assume there are four methods A, B, C, and D. If method A calls method B, method B calls method C, method C calls method D, and method D calls method A, which of the following methods is indirectly recursive?

A) A
B) B
C) C
D) D
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
42
Consider the following definition of a recursive method.public static int mystery(int[] list, int first, int last)
{
If (first == last)
Return list[first];
Else
Return list[first] + mystery(list, first + 1, last);
}Given the declarationint[] alpha = {1, 4, 5, 8, 9};What is the output of the following statement?System.out.println(mystery(alpha, 0, 4));

A) 1
B) 18
C) 27
D) 32
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
43
In the recursive algorithm for the nth Fibonacci number, there are ____ base case(s).

A) zero
B) one
C) two
D) three
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
Consider the following definition of a recursive method.public static int recFunc(int num)
{
If (num >= 10)
Return 10;
Else
Return num * recFunc(num + 1);
}What is the output of the following statement?System.out.println(recFunc(10));

A) 10
B) 110
C) This statement results in infinite recursion.
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
45
Consider the following definition of a recursive method.public static int strange(int[] list, int first, int last)
{
If (first == last)
Return list[first];
Else
Return list[first] + strange(list, first + 1, last);
}Given the declarationint[] beta = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25};What is the output of the following statement?System.out.println(strange(beta, 4, 7));

A) 27
B) 33
C) 55
D) 66
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
46
____ is NOT an iterative control structure.

A) A while loop
B) A for loop
C) Recursion
D) A do...while loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
If you are building a mission control system, ____.

A) you should always use iteration
B) you should always use recursion
C) use the most efficient solution
D) use the solution that makes the most intuitive sense
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
What is the first step in the Tower of Hanoi recursive algorithm?

A) Move the top n disks from needle 1 to needle 2, using needle 3 as the intermediate needle.
B) Move disk number n from needle 1 to needle 3.
C) Move the top n - 1 disks from needle 2 to needle 3, using needle 1 as the intermediate needle.
D) Move the top n - 1 disks from needle 1 to needle 2, using needle 3 as the intermediate needle.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
Using a recursive algorithm to solve the Tower of Hanoi problem, a computer that can generate one billion moves per second would take ____ years to solve the problem.

A) 39
B) 400
C) 500
D) 10,000
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
The third Fibonacci number is ____.

A) the sum of the first two Fibonacci numbers
B) the sum of the last two Fibonacci numbers
C) the product of the first two Fibonacci numbers
D) the product of the last two Fibonacci numbers
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 50 flashcards in this deck.