Deck 11: Recursion

Full screen (f)
exit full mode
Question
The recursive solution of the Towers of Hanoi problem has _______________ complexity.

A) exponential
B) polynomial
C) logarithmic
D) low
E) none of the above
Use Space or
up arrow
down arrow
to flip the card.
Question
A method that calls itself is a __________________ method.

A) invalid
B) static
C) final
D) recursive
E) public
Question
Which of the following is a proper recursive definition of x raised to the y power?

A) xy = x*xy-1
B) xy = x*xy-2
C) xy = x*xy-1 for y > 1; xy = x for y = 1
D) xy = x*xy-1 for all x
E) none of the above
Question
A method that calls a different method, which then calls the original calling method is a recursive method.
Question
Which of the following statements is true?

A) Recursion should be used in every program.
B) Recursion should be avoided all the time.
C) Solutions that are easily expressed recursively should be programmed recursively.
D) Solutions that are easily expressed recursively should be programmed iteratively.
E) None of the above.
Question
In the Towers of Hanoi puzzle __________________________________ .

A) it is legal to move a disk onto a peg that contains smaller disks.
B) it is legal to sit a disk aside, not on any peg.
C) it is illegal to move a disk onto a peg that contains only larger disks.
D) it is illegal to move a disk onto a peg that contains no other disks.
E) none of the above are true
Question
In a recursive solution, _______________ is(are) always necessary.

A) short, efficient code
B) several variables
C) numerous lines of code
D) a base case
E) none of the above
Question
A solution with exponential complexity is ____________________ .

A) efficient
B) inefficient
C) easy to implement
D) difficult to implement
E) none of the above
Question
In the Towers of Hanoi puzzle, there are ________________ pegs.

A) 2
B) 3
C) 4
D) 5
E) The Towers of Hanoi puzzle can include any number of pegs
Question
All recursive methods must have a base case.
Question
All recursive programs ____________________________________ .

A) are more difficult to read than iterative programs.
B) can be implemented iteratively.
C) are easier to read than iterative programs.
D) are shorter than iterative programs.
E) none of the above are true.
Question
There is(are) ____________ base case(s) in the recursive solution to finding a path through a maze.

A) 0
B) 1
C) 2
D) 3
E) 4
Question
Which of the following will result from infinite recursion in Java?

A) The program will hang as though there is an infinite loop.
B) The program will throw an ArrayOutOfBoundsException.
C) The program will not compile.
D) The program will run out of memory.
E) none of the above.
Question
In Java, it is possible for a method to call itself.
Question
The _______________________ puzzle is a favorite of computer scientists because of its elegant recursive solution.

A) Tower of Hanoi
B) Sudoku
C) Tetris
D) Tic-Tac-Toe
E) none of the above
Question
____________________ recursion occurs when a method calls itself, while _________________ recursion when a method calls another method that then calls the original method.

A) upward, downward
B) downward, upward
C) direct, indirect
D) indirect, direct
E) none of the above
Question
Some problems can only be solved recursively.
Question
__________________ recursion results from the lack of a base case.

A) Indirect
B) Direct
C) Infinite
D) Spiral
E) none of the above
Question
In the Towers of Hanoi puzzle there are __________________ disks of different diameters.

A) 2
B) 3
C) 4
D) 5
E) The Towers of Hanoi puzzle can include any number of disks of different diameters.
Question
Recursive solutions to problems should be used whenever possible.
Question
Write a recursive definition for K(n), which represents the product of all of the even integers less than or equal to n.
Question
Describe a recursive solution to the Towers of Hanoi puzzle with N disks.
Question
Describe a recursive solution to determine whether a String object is a palindrome. You may assume that the string only contains lower case characters (i.e. no numbers, spaces, punctuation, etc). Hint: It may be easiest to describe your solution using pseudocode.
Question
Describe the Towers of Hanoi puzzle.
Question
The Towers of Hanoi puzzle cannot be solved iteratively.
Question
A program with infinite recursion will act similarly to a program with an infinite loop.
Question
What is recursion?
Question
Write a recursive method that computes the sum of the first n integers.
Question
Write a recursive method that computes the product of all of the even integers less than or equal to n.
Question
Recursion is necessary.
Question
What is wrong with the following recursive method that computes the sum of all of the odd positive integers less than or equal to n?
public int sumOfOdds(int n) {
if(n%2 == 0)
return sumOfOdds(n-1);
else
return n + sumOfOdds(n-2);
}
Question
Write a recursive Java method that takes in a String as a parameter and returns true if the String is a palindrome. You may assume that the string only contains lower case characters (i.e. no numbers, spaces, punctuation, etc).
Question
The Fibonacci sequence is defined as the sum of the two previous elements of the sequence, with the first and second numbers in the sequence being 0 and 1 respectively. Write a recursive method that computes the nth Fibonacci number.
Question
In the Towers of Hanoi puzzle, it is legal to move a larger disk to a peg that already contains a smaller disk.
Question
Write the recursive definition of the factorial of a number.
Question
Give a recursive definition of the sum of the first n integers, S(n).
Question
What are the "base" cases when searching for a path through a maze?
Question
Determining whether or not there is a path through a maze has a recursive solution.
Question
The Fibonacci sequence is defined as the sum of the two previous elements of the sequence, with the first and second numbers in the sequence being 0 and 1 respectively. Write a recursive mathematical definition of the Fibonacci numbers.
Question
Write a recursive method that returns the factorial of an integer.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 11: Recursion
1
The recursive solution of the Towers of Hanoi problem has _______________ complexity.

A) exponential
B) polynomial
C) logarithmic
D) low
E) none of the above
A
Explanation: The recursive solution of the Towers of Hanoi problem has exponential complexity, which means that it is significantly inefficient.
2
A method that calls itself is a __________________ method.

A) invalid
B) static
C) final
D) recursive
E) public
D
Explanation: A recursive method is a method that calls itself.
3
Which of the following is a proper recursive definition of x raised to the y power?

A) xy = x*xy-1
B) xy = x*xy-2
C) xy = x*xy-1 for y > 1; xy = x for y = 1
D) xy = x*xy-1 for all x
E) none of the above
C
Explanation: Choice c is correct because it has a properly defined base case.
4
A method that calls a different method, which then calls the original calling method is a recursive method.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following statements is true?

A) Recursion should be used in every program.
B) Recursion should be avoided all the time.
C) Solutions that are easily expressed recursively should be programmed recursively.
D) Solutions that are easily expressed recursively should be programmed iteratively.
E) None of the above.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
In the Towers of Hanoi puzzle __________________________________ .

A) it is legal to move a disk onto a peg that contains smaller disks.
B) it is legal to sit a disk aside, not on any peg.
C) it is illegal to move a disk onto a peg that contains only larger disks.
D) it is illegal to move a disk onto a peg that contains no other disks.
E) none of the above are true
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
In a recursive solution, _______________ is(are) always necessary.

A) short, efficient code
B) several variables
C) numerous lines of code
D) a base case
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
A solution with exponential complexity is ____________________ .

A) efficient
B) inefficient
C) easy to implement
D) difficult to implement
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
In the Towers of Hanoi puzzle, there are ________________ pegs.

A) 2
B) 3
C) 4
D) 5
E) The Towers of Hanoi puzzle can include any number of pegs
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
All recursive methods must have a base case.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
All recursive programs ____________________________________ .

A) are more difficult to read than iterative programs.
B) can be implemented iteratively.
C) are easier to read than iterative programs.
D) are shorter than iterative programs.
E) none of the above are true.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
There is(are) ____________ base case(s) in the recursive solution to finding a path through a maze.

A) 0
B) 1
C) 2
D) 3
E) 4
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
Which of the following will result from infinite recursion in Java?

A) The program will hang as though there is an infinite loop.
B) The program will throw an ArrayOutOfBoundsException.
C) The program will not compile.
D) The program will run out of memory.
E) none of the above.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
In Java, it is possible for a method to call itself.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
The _______________________ puzzle is a favorite of computer scientists because of its elegant recursive solution.

A) Tower of Hanoi
B) Sudoku
C) Tetris
D) Tic-Tac-Toe
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
____________________ recursion occurs when a method calls itself, while _________________ recursion when a method calls another method that then calls the original method.

A) upward, downward
B) downward, upward
C) direct, indirect
D) indirect, direct
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
Some problems can only be solved recursively.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
__________________ recursion results from the lack of a base case.

A) Indirect
B) Direct
C) Infinite
D) Spiral
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
In the Towers of Hanoi puzzle there are __________________ disks of different diameters.

A) 2
B) 3
C) 4
D) 5
E) The Towers of Hanoi puzzle can include any number of disks of different diameters.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
Recursive solutions to problems should be used whenever possible.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
Write a recursive definition for K(n), which represents the product of all of the even integers less than or equal to n.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Describe a recursive solution to the Towers of Hanoi puzzle with N disks.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
Describe a recursive solution to determine whether a String object is a palindrome. You may assume that the string only contains lower case characters (i.e. no numbers, spaces, punctuation, etc). Hint: It may be easiest to describe your solution using pseudocode.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
Describe the Towers of Hanoi puzzle.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
The Towers of Hanoi puzzle cannot be solved iteratively.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
A program with infinite recursion will act similarly to a program with an infinite loop.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
What is recursion?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
Write a recursive method that computes the sum of the first n integers.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Write a recursive method that computes the product of all of the even integers less than or equal to n.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
Recursion is necessary.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
What is wrong with the following recursive method that computes the sum of all of the odd positive integers less than or equal to n?
public int sumOfOdds(int n) {
if(n%2 == 0)
return sumOfOdds(n-1);
else
return n + sumOfOdds(n-2);
}
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
Write a recursive Java method that takes in a String as a parameter and returns true if the String is a palindrome. You may assume that the string only contains lower case characters (i.e. no numbers, spaces, punctuation, etc).
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
The Fibonacci sequence is defined as the sum of the two previous elements of the sequence, with the first and second numbers in the sequence being 0 and 1 respectively. Write a recursive method that computes the nth Fibonacci number.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
In the Towers of Hanoi puzzle, it is legal to move a larger disk to a peg that already contains a smaller disk.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
Write the recursive definition of the factorial of a number.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
Give a recursive definition of the sum of the first n integers, S(n).
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
What are the "base" cases when searching for a path through a maze?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
Determining whether or not there is a path through a maze has a recursive solution.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
The Fibonacci sequence is defined as the sum of the two previous elements of the sequence, with the first and second numbers in the sequence being 0 and 1 respectively. Write a recursive mathematical definition of the Fibonacci numbers.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
Write a recursive method that returns the factorial of an integer.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.