Chapter 18 Flashcards
What are the base cases in the following recursive method?
publicstaticvoidxMethod(intn) { if(n >0) { System.out.print(n %10); xMethod(n /10); } } A. n > 0 B. n <= 0 C. no base cases D. n < 0
B.
n <= 0
Analyze the following recursive method.
publicstaticlongfactorial(intn) { returnn * factorial(n -1); } A. Invoking factorial(0) returns 0. B. Invoking factorial(1) returns 1. C. Invoking factorial(2) returns 2. D. Invoking factorial(3) returns 6. E. The method runs infinitely and causes a StackOverflowError.
E.
The method runs infinitely and causes a StackOverflowError.
How many times is the factorial method in Listing 18.1 invoked for factorial(5)?
A. 3 B. 4 C. 5 D. 6
D.
6
Which of the following statements are true?
A. The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series. B. The Fibonacci series begins with 1 and 1, and each subsequent number is the sum of the preceding two numbers in the series. C. The Fibonacci series begins with 1 and 2, and each subsequent number is the sum of the preceding two numbers in the series. D. The Fibonacci series begins with 2 and 3, and each subsequent number is the sum of the preceding two numbers in the series.
A.
The Fibonacci series begins with 0 and 1, and each subsequent number is the sum of the preceding two numbers in the series.
How many times is the fib method in Listing 18.2 invoked for fib(5)?
A. 14 B. 15 C. 25 D. 31 E. 32
B.
15
In the following method, what is the base case?
staticintxMethod(intn){ if(n ==1) return1; else returnn + xMethod(n -1); } A. n is 1. B. n is greater than 1. C. n is less than 1. D. no base case.
A.
n is 1.
What is the return value for xMethod(4) after calling the following method?
staticintxMethod(intn){ if(n ==1) return1; else returnn + xMethod(n -1); } A. 12 B. 11 C. 10 D. 9
C.
10
Fill in the code to complete the following method for checking whether a string is a palindrome.
publicstaticbooleanisPalindrome(Strings){
if(s.length() <=1)// Base case
returntrue;
elseif_____________________________
returnfalse;
else
returnisPalindrome(s.substring(1, s.length() -1));
}
A.
(s.charAt(0) != s.charAt(s.length() - 1)) // Base case
B.
(s.charAt(0) != s.charAt(s.length())) // Base case
C.
(s.charAt(1) != s.charAt(s.length() - 1)) // Base case
D.
(s.charAt(1) != s.charAt(s.length())) // Base case
A.
(s.charAt(0) != s.charAt(s.length() - 1)) // Base case
Analyze the following code:
publicclassTest{ publicstaticvoidmain(String[] args) { int[] x = {1,2,3,4,5}; xMethod(x,5); }
publicstaticvoidxMethod(int[] x,intlength) { System.out.print(" "+ x[length -1]); xMethod(x, length -1); } } A. The program displays 1 2 3 4 6. B. The program displays 1 2 3 4 5 and then raises an ArrayIndexOutOfBoundsException. C. The program displays 5 4 3 2 1. D. The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
D.
The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.
Fill in the code to complete the following method for checking whether a string is a palindrome.
publicstaticbooleanisPalindrome(Strings){
returnisPalindrome(s,0, s.length() -1);
}
publicstaticbooleanisPalindrome(Strings,intlow,inthigh){ if(high <= low)// Base case returntrue; elseif(s.charAt(low) != s.charAt(high))// Base case returnfalse; else return\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; } A. isPalindrome(s) B. isPalindrome(s, low, high) C. isPalindrome(s, low + 1, high) D. isPalindrome(s, low, high - 1) E. isPalindrome(s, low + 1, high - 1)
E.
isPalindrome(s, low + 1, high - 1)
Fill in the code to complete the following method for sorting a list.
publicstaticvoidsort(double[]list){
___________________________;
}
publicstaticvoidsort(double[]list,inthigh){ if(high >1) { // Find the largest number and its index intindexOfMax =0; doublemax = list[0]; for(inti =1; i <= high; i++) { if(list[i] > max) { max = list[i]; indexOfMax = i; } }
// Swap the largest with the last number in the list list[indexOfMax] = list[high]; list[high] = max;
// Sort the remaining list sort(list, high -1); } } A. sort(list) B. sort(list, list.length) C. sort(list, list.length - 1) D. sort(list, list.length - 2)
C.
sort(list, list.length - 1)
Fill in the code to complete the following method for binary search.
publicstaticintrecursiveBinarySearch(int[]list,intkey){ intlow =0; inthigh = list.length -1; return\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_; }
publicstaticintrecursiveBinarySearch(int[]list,intkey, intlow,inthigh) { if(low > high)// The list has been exhausted without a match return-low -1;// Return -insertion point - 1 intmid = (low + high) /2; if(key < list[mid]) returnrecursiveBinarySearch(list, key, low, mid -1); elseif(key == list[mid]) returnmid; else returnrecursiveBinarySearch(list, key, mid +1, high); } A. recursiveBinarySearch(list, key) B. recursiveBinarySearch(list, key, low + 1, high - 1) C. recursiveBinarySearch(list, key, low - 1, high + 1) D. recursiveBinarySearch(list, key, low, high)
D.
recursiveBinarySearch(list, key, low, high)
How many times is the recursive moveDisks method invoked for 3 disks? A. 3 B. 7 C. 10 D. 14
B.
7
How many times is the recursive moveDisks method invoked for 4 disks? A. 5 B. 10 C. 15 D. 20
C.
15
Analyze the following two programs:
A: publicclassTest{ publicstaticvoidmain(String[] args) { xMethod(5); } publicstaticvoidxMethod(intlength) { if(length >1) { System.out.print((length -1) +" "); xMethod(length -1); } } }
B: publicclassTest{ publicstaticvoidmain(String[] args) { xMethod(5); }
publicstaticvoidxMethod(intlength) { while(length >1) { System.out.print((length -1) +" "); xMethod(length -1); } } } A. The two programs produce the same output 5 4 3 2 1. B. The two programs produce the same output 1 2 3 4 5. C. The two programs produce the same output 4 3 2 1. D. The two programs produce the same output 1 2 3 4. E. Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 .... 1 infinitely.
E.
Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 …. 1 infinitely.