Deck 16: 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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/42
Play
Full screen (f)
Deck 16: Recursion
1
To solve a program recursively, you need to identify at least one case in which the problem can be solved without recursion - this is known as the:
A) recursive case
B) terminal case
C) base case
D) final case
A) recursive case
B) terminal case
C) base case
D) final case
C
2
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the depth of Test14(16)?
A) 0
B) 1
C) 2
D) 3
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the depth of Test14(16)?
A) 0
B) 1
C) 2
D) 3
C
3
Look at the following method: public static int test2(int x, int y)
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is the depth of test2(18,5)?
A) 0
B) 1
C) 2
D) 3
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is the depth of test2(18,5)?
A) 0
B) 1
C) 2
D) 3
C
4
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the recursive case for the algorithm?
A) x < 8
B) 2 * x
C) x != 8
D) x >= 8
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the recursive case for the algorithm?
A) x < 8
B) 2 * x
C) x != 8
D) x >= 8
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
5
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the depth of Test14(7)?
A) 0
B) 1
C) 6
D) 7
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the depth of Test14(7)?
A) 0
B) 1
C) 6
D) 7
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
6
Look at the following method: public static int test2(int x, int y)
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is returned for test2(10, 20)?
A) 6
B) 10
C) 1
D) -5
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is returned for test2(10, 20)?
A) 6
B) 10
C) 1
D) -5
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
7
Look at the following method: public static int test2(int x, int y)
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is the recursive case for the method?
A) x < y
B) -5
C) x >= y
D) x != y
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is the recursive case for the method?
A) x < y
B) -5
C) x >= y
D) x != y
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
8
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What value is returned for Test14(7)?
A) 0
B) 7
C) 14
D) -5
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What value is returned for Test14(7)?
A) 0
B) 7
C) 14
D) -5
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
9
Recursion can be a powerful tool for solving:
A) basic problems
B) repetitive problems
C) object-oriented problems
D) binary problems
A) basic problems
B) repetitive problems
C) object-oriented problems
D) binary problems
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
10
Usually, a problem is reduced by making the value of one or more parameters ________ with each recursive call.
A) larger
B) smaller
C) negative
D) equal
A) larger
B) smaller
C) negative
D) equal
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
11
Recursive algorithms are usually less efficient than:
A) iterative algorithms
B) quantum algorithms
C) pseudocode algorithms
D) none of the above
A) iterative algorithms
B) quantum algorithms
C) pseudocode algorithms
D) none of the above
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
12
Like a loop, a recursive method must have:
A) a counter
B) some way to control the number of times it repeats itself
C) a return statement
D) a predetermined number of times it will execute before terminating
A) a counter
B) some way to control the number of times it repeats itself
C) a return statement
D) a predetermined number of times it will execute before terminating
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
13
How many times will the following method call itself if the value 10 is passed as an argument?
Public static void message(int n)
{
If (n > 0)
{
System.out.println("Print this line.\n");
Message(n + 1);
}
}
A) 1
B) 9
C) 10
D) An infinite number of times
Public static void message(int n)
{
If (n > 0)
{
System.out.println("Print this line.\n");
Message(n + 1);
}
}
A) 1
B) 9
C) 10
D) An infinite number of times
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
14
Look at the following method: public static int test2(int x, int y)
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is returned for test2(18,5)?
A) 6
B) -5
C) 7
D) 1
{
If ( x < y)
{
Return -5;
}
Else
{
Return (test2(x - y, y + 5) + 6);
}
}
What is returned for test2(18,5)?
A) 6
B) -5
C) 7
D) 1
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
15
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What value is returned for Test14(16)?
A) 8
B) 16
C) 24
D) 32
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What value is returned for Test14(16)?
A) 8
B) 16
C) 24
D) 32
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
16
The depth of recursion is:
A) the number of times that a method calls itself
B) the value returned from the last recursive call
C) the value that will terminate the recursive calls
D) The order in which the method appears on the stack
A) the number of times that a method calls itself
B) the value returned from the last recursive call
C) the value that will terminate the recursive calls
D) The order in which the method appears on the stack
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
17
A recursive method is:
A) a method that accepts no arguments
B) a method that contains a static field
C) a method that calls itself
D) a method that has a loop
A) a method that accepts no arguments
B) a method that contains a static field
C) a method that calls itself
D) a method that has a loop
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
18
The actions that the JVM must perform any time a method is called are known as:
A) Stack depth
B) Overhead
C) Housekeeping
D) Method calls
A) Stack depth
B) Overhead
C) Housekeeping
D) Method calls
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
19
Look at the following method: public static int Test2(int x, int y)
{
If ( x < y)
{
Return -5;
}
Else
{
Return (Test2(x - y, y + 5) + 6);
}
}
What is the base case for the method?
A) x < y
B) -5
C) Test2(x - y, y + 5) + 6
D) +6
{
If ( x < y)
{
Return -5;
}
Else
{
Return (Test2(x - y, y + 5) + 6);
}
}
What is the base case for the method?
A) x < y
B) -5
C) Test2(x - y, y + 5) + 6
D) +6
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
20
Look at the following pseudocode algorithm: algorithm Test14(int x)
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the base case for the algorithm?
A) x < 8
B) 2 * x
C) 3 * Test14(x - 8) + 8
D) x >= 8
If (x < 8)
Return (2 * x)
Else
Return (3 * Test14(x - 8) + 8)
End Test14
What is the base case for the algorithm?
A) x < 8
B) 2 * x
C) 3 * Test14(x - 8) + 8
D) x >= 8
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
21
Indirect recursion occurs when:
A) a loop is used to solve a recursive problem
B) a recursive method has no code to stop it from repeating
C) a method calls itself
D) a method calls itself from another method
A) a loop is used to solve a recursive problem
B) a recursive method has no code to stop it from repeating
C) a method calls itself
D) a method calls itself from another method
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
22
A problem can be solved recursively if it can be broken down into successive smaller problems that are unique within the overall problem.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
23
Look at the following pseudocode algorithm: Algorithm gcd(x, y)
If (x < y)
Gcd (y, x)
Else
If (y = 0)
Return x
Else
Return gcd(y, x mod y)
End gcd
What is returned from gcd(60, 24)?
A) 60
B) 24
C) 12
D) 66
If (x < y)
Gcd (y, x)
Else
If (y = 0)
Return x
Else
Return gcd(y, x mod y)
End gcd
What is returned from gcd(60, 24)?
A) 60
B) 24
C) 12
D) 66
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
24
Look at the following pseudocode algorithm: Algorithm Test3(int a, int b)
If (a < b)
Return 5
Else if ( a == b)
Return -5;
Else
Return (a + Test3(a - 1, b)
End Test3
What is the base case for the algorithm?
A) a < b
B) a == b
C) Both A and B
D) neither A nor B
If (a < b)
Return 5
Else if ( a == b)
Return -5;
Else
Return (a + Test3(a - 1, b)
End Test3
What is the base case for the algorithm?
A) a < b
B) a == b
C) Both A and B
D) neither A nor B
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
25
If the base case in a recursive method is never reached:
A) the method will call itself only once
B) the result will always be off by one
C) the method will call itself indefinitely
D) the method will never call itself
A) the method will call itself only once
B) the result will always be off by one
C) the method will call itself indefinitely
D) the method will never call itself
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
26
The Towers of Hanoi is:
A) a mathematical game
B) often used in computer science textbooks
C) demonstrates the power of recursion
D) all of the above
A) a mathematical game
B) often used in computer science textbooks
C) demonstrates the power of recursion
D) all of the above
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
27
The number of times that a method calls itself is known as the:
A) height of recursion
B) depth of recursion
C) width of recursion
D) length of recursion
A) height of recursion
B) depth of recursion
C) width of recursion
D) length of recursion
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
28
Any problem that can be solved recursively can also be solved iteratively.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
29
How many times will the following method call itself if the value 10 is passed as the argument?
Public static void message(int n)
{
If (n < 0)
{
System.out.println("Print this line.\n");
Message(n + 1);
}
}
A) 0
B) 9
C) 10
D) An infinite number of times
Public static void message(int n)
{
If (n < 0)
{
System.out.println("Print this line.\n");
Message(n + 1);
}
}
A) 0
B) 9
C) 10
D) An infinite number of times
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
30
Look at the following pseudocode algorithm: Algorithm Test3(int a, int b)
If (a < b)
Return 5
Else if ( a == b)
Return -5;
Else
Return (a + Test3(a - 1, b)
End Test3
What is the recursive case for the algorithm?
A) a < b
B) a == b
C) a > b
D) None of the above
If (a < b)
Return 5
Else if ( a == b)
Return -5;
Else
Return (a + Test3(a - 1, b)
End Test3
What is the recursive case for the algorithm?
A) a < b
B) a == b
C) a > b
D) None of the above
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
31
This type of method is a method that calls itself.
A) Looping
B) Circular
C) Recursive
D) Reoccurring
A) Looping
B) Circular
C) Recursive
D) Reoccurring
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
32
Indirect recursion occurs when a method calls another method that in turn calls the first method.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
33
Look at the following pseudocode algorithm: Algorithm gcd(x, y)
If (x < y)
Gcd (y, x)
Else
If (y = 0)
Return x
Else
Return gcd(y, x mod y)
End gcd
What is the base case for the algorithm gcd?
A) x < y
B) y == 0
C) x == 0
D) y > x
If (x < y)
Gcd (y, x)
Else
If (y = 0)
Return x
Else
Return gcd(y, x mod y)
End gcd
What is the base case for the algorithm gcd?
A) x < y
B) y == 0
C) x == 0
D) y > x
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
34
Which of the following problems can be programmed recursively?
A) Towers of Hanoi
B) Quicksort
C) Binary search
D) All of the above
A) Towers of Hanoi
B) Quicksort
C) Binary search
D) All of the above
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
35
This term is used for methods that directly call themselves.
A) Direct recursion
B) Simple recursion
C) Absolute recursion
D) Native recursion
A) Direct recursion
B) Simple recursion
C) Absolute recursion
D) Native recursion
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
36
In Java, it is not possible for a method to call itself.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
37
Recursive algorithms are usually less efficient than iterative algorithms.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
38
The part of a problem that is solved with recursion is known as the:
A) terminal case
B) final case
C) absolute case
D) recursive case
A) terminal case
B) final case
C) absolute case
D) recursive case
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
39
Like ________, a recursive method must have some way to control the number of times it repeats.
A) a loop
B) any method
C) a class constructor
D) an event
A) a loop
B) any method
C) a class constructor
D) an event
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
40
Unlike a loop, a recursive method does not require code to stop it from repeating.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
41
If method A calls method B, which in turn calls method A, it is called indirect recursion.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck
42
A problem can be solved recursively if it can be broken down into successive smaller problems that are identical to the overall problem.
Unlock Deck
Unlock for access to all 42 flashcards in this deck.
Unlock Deck
k this deck