Questions Chapter 6 Flashcards
If the user enters 10 in the triangle.java program (Listing 6.1), what is the maximum number of “copies” of the triangle( ) method (actually just copies of its argument) that exist at any one time?
10
Where are the copies of the argument in the triangle.java program stored?
a. in a variable in the triange( ) method
b. in a field of the TriangleApp class
c. in a variable of the getString( ) method
d. on a stack
d. on a stack.
Assume the user enters10 in the triangle.java program. What is the value of n when the triangle ( ) method first returns a value other than 1?
2
Assume the user enters 10 in the triangle. java program. What is the value of n when the triangle method is about to return to main( )?
10
In the anagram.java program (Listing 6.2), at a certain depth of recursion, a version of the doAnagram( ) method is working with the string “led”. When this method calls a new version of itself, what letters will the new version be working with?
ed
True or False: In the triangle ( ) method, the return values are store on the stack.
False
We’ve seen that recursion can take the place of a loop, as in the loop-oriented orderedArray.java program (Listing 2.4) and the recursive binarySearch.java program (Listing 6.3). Which of the following is NOT true?
a. Both programs divide the range repeatedly in half
b. if the key is not found, the loop version returns because the range bounds cross, but the recursive version occurs because it reaches the bottom recursion level.
c. if the key is found, the loop version returns from the entire method, whereas the recursive version returns from only one level of recursion.
d. in the recursive version, the range to be searched must be specified in the arguments, while in the loop version it need not be.
b
In the recFind( ) method in the binarySearch.java program (Listing 6.3), what takes the place of the loop in the non-recursive version?
a. the recFind( ) method
b. arguments to recFind( )
c. recursive calls to recFind( )
d. the call from main( ) to recFind( )
c. recursive calls to recFind( )
The binarySearch.java program is an example of the _______________ approach to solving a problem.
divide and conquer approach
What gets smaller as you make repeated recursive calls in the towers.java program (Listing 6.4)?
top
What becomes smaller as you make repeated recursive calls in the recFind( ) method?
range to search
The algorithm in the towers.java program involves:
a. “trees” that are data storage devices
b. secretly putting small disks under large disks
c. changing which columns are the source and destination
d. moving one small disk and then a stack of larger disks
c. changing which columns are the source and destination
Which is NOT true about the merge( ) method in the merge.java program (Listing 6.5)?
a. its algorithm can handle arrays of different sizes
b. it must search the target array to find where to put the next item
c. it is not recursive
d. it continuously takes the smallest item irrespective of what array it’s in.
b. it must search the target array to find where to put the next item
The disadvantage of mergesort is that:
a. it is not recursive
b. it uses more memory
c. although faster than the insertion sort, it is much slower than quicksort
d. it is complicated to implement
b. it uses more memory
Besides a loop, a ________ can often be used instead of recursion.
stack