Content Frame
Note for screen reader users: There is text between the form elements on this page. To be sure that you do not miss any text, use item by item navigation methods, rather than tabbing from form element to form element.
Skip Breadcrumb Navigation
Home  arrow Student Resources  arrow Chapter Quizzes  arrow Chapter 11

Chapter 11

Recursion

This activity contains 15 questions.

Question 1.
This method is supposed to return the reverse of the string it is given. Which is the correct statement to insert at the indicated line?
String reverse(String s) {
	String result;
	if (s.length() <= 1)
		result = s;
	else
		// What line to insert here?
	return result;
}


Open Hint for Question 1 in a new window.
 
End of Question 1


Question 2.

This method is supposed to return true if the String it is given is palindromic (reads the same front-to-back and back-to-front), and returns false otherwise.

What's the missing statement?
boolean palindromic(String s) {
	boolean palin;
	int last = s.length() - 1;
	if (s.length() <= 1)
		palin = true;
	else if // missing statement goes here
	else
		palin = false;	
	return palin;
}

Open Hint for Question 2 in a new window.
 
End of Question 2


Question 3.

What does this program print?
public class NFactorial {

	NFactorial() {
		for (long n = 1; n <= 6; n++)
			System.out.println(n + "! = " +
				factorial(n));
	}

	long factorial(long n) {
		long nfactorial = 
			(n <= 1) ? 1 : n*factorial(n-1);
		return nfactorial;
	}
	
	public static void main(String[] args) {
		new NFactorial();
	}
}

Open Hint for Question 3 in a new window.
 
End of Question 3


Question 4.

What does this program print?
public class Recursive {

	Recursive() {
		System.out.println(calcA(3, 4));
	}

	int calcA(int a, int b) {
		return (a * b == 0) ? 0 :
					calcB(calcA(a, b-1), b);
	}

	int calcB(int a, int b) {
		return (b <= 0) ? a : 
						  1 + calcB(a, b-1);
	}

	public static void main(String[] args) {
		new Recursive();
	}
}

Open Hint for Question 4 in a new window.
 
End of Question 4


Question 5.

What is printed?
public class Recurse {

	Recurse() {
		int[] a = { 12, 23, 34, 45,
				    56, 67, 78 };
		System.out.println(
			search(a, 0, a.length-1, 40)
			+ " " +
			search(a, 0, a.length-1, 56));
	}

	int search(int[] a, 
			int frst, int last, int key) {
		int mid = (frst + last) / 2;
		return 
			(frst > last)   ? -1 :
			(key == a[mid]) ? a[mid] :
			(key <  a[mid]) ? 
			   search(a, frst, mid-1, key) :
			   search(a, mid+1, last, key);
	}

	public static void main(String[] args) {
		new Recurse();
	}

}

Open Hint for Question 5 in a new window.
 
End of Question 5


Question 6.

What is printed?
public class Recurse {

	Recurse() {
		int[] a = { 3, 1, 4, 1, 
					5, 9, 2, 6, 5 };
		System.out.println(
					find(a, a.length - 1));
	}

	int find(int[] a, final int n) {
		int ans = 0;
		if (n >= 0) {
			ans = ma(a[n], find(a, n-1));
		}
		return ans;
	}

	int ma(final int a, final int b) {
		return (a > b) ? a : b;
	}

	public static void main(String[] args) {
		new Recurse();
	}

}

Open Hint for Question 6 in a new window.
 
End of Question 6


Question 7.

What is printed?
public class Recurse {

	Recurse() {
		System.out.println(gc(18, 27));
		System.out.println(gc(61, 53));
		System.out.println(gc(98, 868));
	}

	int gc(final int a, final int b) {
		return (b == 0) ? a : gc(b, a % b);
	}

	public static void main(String[] args) {
		new Recurse();
	}

}

Open Hint for Question 7 in a new window.
 
End of Question 7


Question 8.

What is printed?
public class Recurse {

	Recurse() {
		System.out.println(pw(3, 5));
	}

	int pw(final int a, final int b) {
		int c = 1;
		if (b > 0) {
			if (b % 2 == 0)
				c = pw(a * a, b / 2);
			else {
				c = a * pw(a, b - 1);
			}
		}
		return c;
	}

	public static void main(String[] args) {
		new Recurse();
	}

}

Open Hint for Question 8 in a new window.
 
End of Question 8


Question 9.

What is printed?
public class Recurse {

	Recurse() {
		System.out.println(dog("abcd",
							   "EFG"));
	}

	String dog(String s, String t) {
		String ans = "";
		if (t == null || t.length() <= 0)
			ans = s;
		else
			ans = dog(s + 
					 t.charAt(t.length()-1),
					 t.substring(0, 
							 t.length()-1));
		return ans;
	}

	public static void main(String[] args) {
		new Recurse();
	}

}

Open Hint for Question 9 in a new window.
 
End of Question 9


Question 10.

What is printed?
public class Recursive {

	Recursive() {
		for (int n = 1; n < 3; n++)
			for (int k = 1; k < 3; k++)
				System.out.println(
					n + " " + k + " = " +
					calc(n, k));
	}

	int calc(int a, int b) {
		int result = a;
		if (b > 0)
			result = calc(a+1, b-1);
		return result;
	}
	
	public static void main(String[] args) {
		new Recursive();
	}
}

Open Hint for Question 10 in a new window.
 
End of Question 10


Question 11.

What is printed?
public class Recursive {

	Recursive() {
		System.out.println(calcA(3, 4));
	}

	int calcA(int a, int b) {
		return (a * b == 0) ? 0 :
					calcB(calcA(a-1, b), b);
	}

	int calcB(int a, int b) {
		return (b <= 0) ? a : 
							calcB(a+1, b-1);
	}

	public static void main(String[] args) {
		new Recursive();
	}
}

Open Hint for Question 11 in a new window.
 
End of Question 11


Question 12.

What is printed?
public class Recursive {

	Recursive() {
		System.out.println(Ack(2, 3));
	}
		
	long Ack(long m, long n) {
		long result;
		if (m <= 0)
			result = n+1;
		else if (n <= 0)
			result = Ack(m-1, 1);
		else
			result = Ack(m-1, Ack(m, n-1));
		return result;
	}

	public static void main(String[] args) {
		new Recursive();
	}
}

Open Hint for Question 12 in a new window.
 
End of Question 12


Question 13.

What is printed?
public class Recursive {

	Recursive() {
		System.out.println(Back(2, 3));
	}
		
	long Back(long m, long n) {
		return (m <= 0) ? n+1 :
			   (n <= 0) ? Back(m-1, 1) :
				     Back(m-1,Back(m, n-1));
	}

	public static void main(String[] args) {
		new Recursive();
	}
}

Open Hint for Question 13 in a new window.
 
End of Question 13


Question 14.
What is printed?
public class Recursive {

	Recursive() {
		System.out.println(fib(7));
	}

	int fib(int n) {
		return (n <= 1) ? n :
			   fib(n-1) + fib(n-2);
	}

	public static void main(String[] args) {
		new Recursive();
	}
}


Open Hint for Question 14 in a new window.
 
End of Question 14


Question 15.

Which statements are true?
  1. Direct recursion occurs when a method calls itself.
  2. Indirect recursion occurs when a method calls itself.
  3. Indirect recursion requires the existence of at least two methods.
  4. Direct recursion can experience infinite recursion, while indirect recursion cannot.
  5. Indirect recursion can experience infinite recursion, while direct recursion cannot.

Open Hint for Question 15 in a new window.
 
End of Question 15





Pearson Copyright © 1995 - 2010 Pearson Education . All rights reserved. Pearson Addison Wesley is an imprint of Pearson .
Legal Notice | Privacy Policy | Permissions

Return to the Top of this Page