Deck 3: Recursion: the Mirrors
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
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/60
Play
Full screen (f)
Deck 3: Recursion: the Mirrors
1
A recursive method that computes the number of groups of k out of n things has the precondition that ______.
A)n is a positive number and k is a nonnegative number
B)n is a nonnegative number and k is a positive number
C)n and k are nonnegative numbers
D)n and k are positive numbers
A)n is a positive number and k is a nonnegative number
B)n is a nonnegative number and k is a positive number
C)n and k are nonnegative numbers
D)n and k are positive numbers
C
2
A ______ is a mathematical formula that generates the terms in a sequence from previous terms.
A)local environment
B)pivot item
C)base case
D)recurrence relation
A)local environment
B)pivot item
C)base case
D)recurrence relation
D
3
The factorial of zero is ______.
A)-1
B)0
C)1
D)2
A)-1
B)0
C)1
D)2
C
4
What would happen if a negative value is passed to a method that returns the factorial of the value passed to it?
A)the method will calculate the correct value for the factorial of the number
B)the method will calculate a negative value for the factorial of the number
C)the method would terminate immediately
D)an infinite sequence of recursive calls will occur
A)the method will calculate the correct value for the factorial of the number
B)the method will calculate a negative value for the factorial of the number
C)the method would terminate immediately
D)an infinite sequence of recursive calls will occur
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
5
In the box trace for a recursive method,a new box is created each time ______.
A)the method is called
B)the method returns a value
C)the object is created
D)the object is initialized
A)the method is called
B)the method returns a value
C)the object is created
D)the object is initialized
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
6
In a recursive method that writes a string of characters in reverse order,the base case is ______.
A)a string with a length of 0
B)a string whose length is a negative number
C)a string with a length of 3
D)a string that is a palindrome
A)a string with a length of 0
B)a string whose length is a negative number
C)a string with a length of 3
D)a string that is a palindrome
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following is NOT a precondition for an array that is to be sorted by a recursive binary search algorithm? (first is the index of the first item in the array,last is the index of the last item in the array,and SIZE is the maximum size of the array)
A)SIZE <= first
B)0 <= first
C)last <= SIZE - 1
D)anArray[first] <= anArray[first + 1] <= … <= anArray[last]
A)SIZE <= first
B)0 <= first
C)last <= SIZE - 1
D)anArray[first] <= anArray[first + 1] <= … <= anArray[last]
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
8
How many bases cases will be required in a recursive solution that solves a problem by solving two smaller problems of the same type?
A)0
B)1
C)2
D)3
A)0
B)1
C)2
D)3
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
9
A class method is defined as ______.
A)static
B)abstract
C)private
D)protected
A)static
B)abstract
C)private
D)protected
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
10
The base case for a recursive definition of the factorial of n is ______.
A)factorial (-1)
B)factorial (0)
C)factorial (1)
D)factorial (n - 1)
A)factorial (-1)
B)factorial (0)
C)factorial (1)
D)factorial (n - 1)
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
11
The factorial of n is equal to ______.
A)n - 1
B)n - factorial (n-1)
C)factorial (n-1)
D)n * factorial (n-1)
A)n - 1
B)n - factorial (n-1)
C)factorial (n-1)
D)n * factorial (n-1)
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
12
If the value being searched for by a recursive binary search algorithm is in the array,which of the following is true?
A)the algorithm cannot return a nonpositive number
B)the algorithm cannot return a nonnegative number
C)the algorithm cannot return a zero
D)the algorithm cannot return a negative number
A)the algorithm cannot return a nonpositive number
B)the algorithm cannot return a nonnegative number
C)the algorithm cannot return a zero
D)the algorithm cannot return a negative number
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
13
When you solve a problem by solving two or more smaller problems,each of the smaller problems must be ______ the base case than the original problem.
A)closer to
B)farther to
C)either closer to or the same "distance" from
D)either farther to or the same "distance" from
A)closer to
B)farther to
C)either closer to or the same "distance" from
D)either farther to or the same "distance" from
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
14
The midpoint of a sorted array can be found by ______,where first is the index of the first item in the array and last is the index of the last item in the array.
A)first / 2 + last / 2
B)first / 2 - last / 2
C)(first + last)/ 2
D)(first - last)/ 2
A)first / 2 + last / 2
B)first / 2 - last / 2
C)(first + last)/ 2
D)(first - last)/ 2
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
15
An array is a(n)______.
A)class
B)method
C)object
D)variable
A)class
B)method
C)object
D)variable
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
16
In a recursive solution,the ______ terminates the recursive processing.
A)local environment
B)pivot item
C)base case
D)recurrence relation
A)local environment
B)pivot item
C)base case
D)recurrence relation
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
17
In the box trace,each box roughly corresponds to a(n)______.
A)recursive relation
B)activation record
C)base case
D)pivot item
A)recursive relation
B)activation record
C)base case
D)pivot item
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
18
In the box trace,each box contains all of the following EXCEPT ______.
A)the values of the references and primitive types of the method's arguments
B)the method's local variables
C)the method's class variables
D)a placeholder for the value returned by each recursive call from the current box
E)the value of the method itself
A)the values of the references and primitive types of the method's arguments
B)the method's local variables
C)the method's class variables
D)a placeholder for the value returned by each recursive call from the current box
E)the value of the method itself
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
19
Which of the following is a precondition for a method that accepts a number n and computes the nth Fibonacci number?
A)n is a negative integer
B)n is a positive integer
C)n is greater than 1
D)n is an even integer
A)n is a negative integer
B)n is a positive integer
C)n is greater than 1
D)n is an even integer
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
20
The number of ways to choose k out of n things is ______.
A)the number of ways to choose k - 1 out of n - 1 things
B)the number of ways to choose k out of n - 1 things
C)the sum of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things
D)the product of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things
A)the number of ways to choose k - 1 out of n - 1 things
B)the number of ways to choose k out of n - 1 things
C)the sum of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things
D)the product of the number of ways to choose k - 1 out of n - 1 things and the number of ways to choose k out of n - 1 things
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
21
A recursive solution can have more than one base cases.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
22
In the recursive solution to the Towers of Hanoi problem,the number of disks to move ______ at each recursive call.
A)decreases by 1
B)increases by 1
C)decreases by half
D)increases by half
A)decreases by 1
B)increases by 1
C)decreases by half
D)increases by half
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
23
The binary search algorithm can be applied to an unsorted array.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
24
A recursive solution that finds the factorial of n generates ______ recursive calls.
A)n-1
B)n
C)n+1
D)n*2
A)n-1
B)n
C)n+1
D)n*2
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
25
In the Fibonacci sequence,which of the following integers comes after the sequence 1,1,2,3?
A)3
B)4
C)5
D)6
A)3
B)4
C)5
D)6
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
26
An iterative method always calls itself.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
27
Every recursive method must have a base case.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
28
When constructing a recursive solution,you should assume that a recursive call's postcondition is true if its precondition is true.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
29
The base case for a recursive solution to the kth smallest item problem cannot be predicted in advance.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
30
A recursive solution solves a problem by solving a smaller instance of the same problem.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
31
A binary search starts at the beginning of the collection of items.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
32
For anArray = <2,3,5,6,9,13,16,19>,what is the value returned by a recursive binary search algorithm if the value being searched for is 10?
A)-1
B)0
C)1
D)10
A)-1
B)0
C)1
D)10
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
33
A recursive solution that finds the factorial of n always reduces the problem size by ______ at each recursive call.
A)1
B)2
C)half
D)one-third
A)1
B)2
C)half
D)one-third
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
34
In the recursive solution to the kth smallest item problem,the problem size decreases by ______ at each recursive call.
A)1
B)at least 1
C)half
D)at least half
A)1
B)at least 1
C)half
D)at least half
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
35
A recursive binary search algorithm always reduces the problem size by ______ at each recursive call.
A)1
B)2
C)half
D)one-third
A)1
B)2
C)half
D)one-third
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
36
A method that is declared as static is an object method.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
37
In a sorted array,the kth smallest item is given by ______.
A)anArray[k-1]
B)anArray[k]
C)anArray[SIZE-k]
D)anArray[SIZE+k]
A)anArray[k-1]
B)anArray[k]
C)anArray[SIZE-k]
D)anArray[SIZE+k]
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
38
An iterative solution involves loops.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
39
For anArray = <2,3,5,6,9,13,16,19>,what is the value returned by a recursive binary search algorithm if the value being searched for is 6?
A)1
B)3
C)4
D)6
A)1
B)3
C)4
D)6
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
40
Which of the following is a base case for a recursive binary search algorithm? (first is the index of the first item in the array,last is the index of the last item in the array,and mid is the midpoint of the array).
A)last > first
B)first > last
C)0 <= first
D)last <= SIZE-1
A)last > first
B)first > last
C)0 <= first
D)last <= SIZE-1
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
41
Why do some compilers automatically replace tail recursion with iteration?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
42
What are the two factors which contribute to the inefficiency of some recursive solutions?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
43
What is the box trace?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
44
What is the base case for the recursive solution to the Towers of Hanoi problem?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
45
What is an activation record?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
46
Why does the Fibonacci sequence have two base cases? In other words,would it be possible to write a correct recursive Fibonacci method that has only one base case?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
47
Write a recursive method that takes 3 parameters: an integer array a,and two integers first and last.The method will find the largest value in a between indices first and last,inclusive.That is,it will return the largest value in the part of the array a[first..last] .You may assume that first last.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
48
How does a sequential search work?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
49
What is a tail-recursive method?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
50
Suppose a program contains a recursive method findFibonacci(int n),which computes the n-th Fibonacci number.Even if we know that a client method will never call findFibonacci( )with the values 1 or 2 as arguments,why does the implementation of findFibonacci( )still need to have base cases?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
51
What is a base case?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
52
What are the four questions that must be considered when constructing a recursive solution?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
53
When is the base case value == anArray[mid] (where mid is the midpoint of the array)reached in a recursive binary search algorithm?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
54
When is the base case first > last (where first is the index of the first item in the array and last is the index of the last item in the array)reached in a recursive binary search algorithm?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
55
What elements are included in a method's local environment?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
56
What is a pivot item?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
57
Suppose sa is a sorted array of integer values,and we wish to search for a number target in this array.If sa contains n elements,and we use the binary search strategy,what is the approximate maximum number of comparisons necessary to determine that the value target is or is not in the array? How would your answer change if the array were not sorted?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
58
Write a recursive method that takes a String parameter and prints out the characters of the string in reverse order.
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
59
What is a recurrence relation?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck
60
What are the two base cases for a recursive binary search algorithm?
Unlock Deck
Unlock for access to all 60 flashcards in this deck.
Unlock Deck
k this deck