Chapter 18 Flashcards

1
Q

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
A

B.

n <= 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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.
A

E.

The method runs infinitely and causes a StackOverflowError.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How many times is the factorial method in Listing 18.1 invoked for factorial(5)?

		A.
3
		B.
4
		C.
5
		D.
6
A

D.

6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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
A

B.

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A.

n is 1.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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
A

C.

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

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

A.

(s.charAt(0) != s.charAt(s.length() - 1)) // Base case

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.
A

D.

The program displays 5 4 3 2 1 and then raises an ArrayIndexOutOfBoundsException.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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)
A

E.

isPalindrome(s, low + 1, high - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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)
A

C.

sort(list, list.length - 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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)
A

D.

recursiveBinarySearch(list, key, low, high)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
How many times is the recursive moveDisks method invoked for 3 disks?
		A.
3
		B.
7
		C.
10
		D.
14
A

B.

7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
How many times is the recursive moveDisks method invoked for 4 disks?
		A.
5
		B.
10
		C.
15
		D.
20
A

C.

15

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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.
A

E.

Program A produces the output 4 3 2 1 and Program B prints 4 3 2 1 1 1 …. 1 infinitely.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Analyze the following functions;

	publicclassTest1{
	publicstaticvoidmain(String[] args) {
	System.out.println(f1(3));
	System.out.println(f2(3,0));
	}
	
	publicstaticintf1(intn) {
	if(n ==0)
	return0;
	else{
	returnn + f1(n -1);
	}
	}
	publicstaticintf2(intn,intresult) {
	if(n ==0)
	returnresult;
	else
	returnf2(n -1, n + result);
	}
	}
		A.
f1 is tail recursion, but f2 is not
		B.
f2 is tail recursion, but f1 is not
		C.
f1 and f2 are both tail recursive
		D.
Neither f1 nor f2 is tail recursive
A

B.

f2 is tail recursion, but f1 is not

17
Q

Show the output of the following code

publicclassTest1{
publicstaticvoidmain(String[] args) {
System.out.println(f2(2,0));
}
	publicstaticintf2(intn,intresult) {
	if(n ==0)
	return0;
	else
	returnf2(n -1, n + result);
	}
	}
		A.
0
		B.
1
		C.
2
		D.
3
A

A.

0