Deck 13: Recursion
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/50
Play
Full screen (f)
Deck 13: Recursion
1
The process of solving a problem by reducing it to smaller versions of itself is called recursion.
True
2
A recursive method in which the first statement executed is a recursive call is called a tail recursive method.
False
3
A program will terminate after completing any particular recursive call.
False
4
The base case starts the recursion.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
5
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
6
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
7
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
8
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
9
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
10
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
11
In the base case of a recursive solution, the solution is obtained through a call to a smaller version of the original method.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
12
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
13
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
14
Every recursive call has its own code.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
Which of the following is an invalid call to the method in the accompanying figure?
A) exampleRecursion(-1)
C) exampleRecursion(1)
B) exampleRecursion( 0)
D) exampleRecursion(999)
A) exampleRecursion(-1)
C) exampleRecursion(1)
B) exampleRecursion( 0)
D) exampleRecursion(999)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
24
What is the output of exampleRecursion(3)?
A) 25
C) 36
B) 32
D) 42
A) 25
C) 36
B) 32
D) 42
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
25
What is the output of exampleRecursion(0)?
A) 0
C) 9
B) 3
D) 27
A) 0
C) 9
B) 3
D) 27
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
26
Recursive algorithms are implemented using while loops.
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
27
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)
C) func1(1, 2)
B) func1(1, 1)
D) func1(2, 0)
A) func1(1, 0)
C) func1(1, 2)
B) func1(1, 1)
D) func1(2, 0)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
28
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
C) Statements in Lines 3, 4, and 5
B) Statements in Lines 5 and 6
D) None of these
A) Statements in Lines 3 and 4
C) Statements in Lines 3, 4, and 5
B) Statements in Lines 5 and 6
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
29
What precondition must exist in order to prevent the code in the accompanying figure from infinite recursion?
A) m >= 0 and n >= 0
C) m >= 1 and n >= 0
B) m >= 0 and n >= 1
D) m >= 1 and n >= 1
A) m >= 0 and n >= 0
C) m >= 1 and n >= 0
B) m >= 0 and n >= 1
D) m >= 1 and n >= 1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
30
What is the limiting condition of the code in the accompanying figure?
A) n >= 0
C) m >= 0
B) m > n
D) n > m
A) n >= 0
C) m >= 0
B) m > n
D) n > m
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
31
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
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
32
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
33
How many base cases are in the code in the accompanying figure?
A) zero
C) two
B) one
D) three
A) zero
C) two
B) one
D) three
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
34
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)
C) Only (iii)
B) Only (ii)
D) Both (i) and (iii)
A) Only (i)
C) Only (iii)
B) Only (ii)
D) Both (i) and (iii)
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
35
What is the limiting condition of the code in the accompanying figure?
A) n >= 0
C) n > 1
B) n > 0
D) n >= 1
A) n >= 0
C) n > 1
B) n > 0
D) n >= 1
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
36
What is the output of func2(2, 3)?
A) 2
C) 5
B) 3
D) 6
A) 2
C) 5
B) 3
D) 6
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
37
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
38
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.
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
39
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
C) 720
B) 72
D) None of these
A) 8
C) 720
B) 72
D) None of these
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
40
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
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
41
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
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
42
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
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
43
In the recursive algorithm for the nth Fibonacci number, there are ____ base case(s).
A) zero
C) two
B) one
D) three
A) zero
C) two
B) one
D) three
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
44
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.
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
45
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
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
46
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 declaration int[] alpha = {1, 4, 5, 8, 9}; What is the output of the following statement? System.out.println(mystery(alpha, 0, 4));
A) 1
C) 27
B) 18
D) 32
A) 1
C) 27
B) 18
D) 32
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
47
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
C) C
B) B
D) D
A) A
C) C
B) B
D) D
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
48
____ is NOT an iterative control structure.
A) A while loop
C) Recursion
B) A for loop
D) A do...while loop
A) A while loop
C) Recursion
B) A for loop
D) A do...while loop
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
49
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 declaration int[] 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
C) 55
B) 33
D) 66
A) 27
C) 55
B) 33
D) 66
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck
50
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
C) 500
B) 400
D) 10,000
A) 39
C) 500
B) 400
D) 10,000
Unlock Deck
Unlock for access to all 50 flashcards in this deck.
Unlock Deck
k this deck