Chapter 14 Check Point Questions
Section 14.2
▼14.2.1
What is a recursive method? What is an infinite recursion?
▼14.2.2
How many times is the factorial method in Listing 18.1 invoked for factorial(6)?
▼14.2.3
Show the output of the following programs and identify base cases and recursive calls.
(a) public class Test { public static void main(String[] args) { System.out.println( "Sum is " + xMethod(5)); } public static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } } (b) public class Test { public static void main(String[] args) { xMethod(1234567); } public static void xMethod(int n) { if (n > 0) { System.out.print(n % 10); xMethod(n / 10); } } }
▼14.2.4
Write a recursive mathematical definition for computing 2 n for a positive integer n.
▼14.2.5
Write a recursive mathematical definition for computing x n for a positive integer n and a real number x.
▼14.2.6
Write a recursive mathematical definition for computing 1 + 2 + 3 + ... + n for a positive integer n.
Section 14.3
▼14.3.1
Show the output of the following two programs:
(a) public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int n) { if (n > 0) { System.out.print(n + " "); xMethod(n - 1); } } } (b) public class Test { public static void main(String[] args) { xMethod(5); } public static void xMethod(int n) { if (n > 0) { xMethod(n - 1); System.out.print(n + " "); } } }
▼14.3.2
What is wrong in the following method?
(a) public class Test { public static void main(String[] args) { xMethod(1234567); } public static void xMethod(double n) { if (n != 0) { System.out.print(n); xMethod(n / 10); } } } (b) public class Test { public static void main(String[] args) { Test test = new Test(); System.out.println(test.toString()); } public Test() { Test test = new Test(); } }
▼14.3.3
How many times is the fib method in Listing 18.2 invoked for fib(6)?
Section 14.4
▼14.4.1
Describe the characteristics of recursive methods.
▼14.4.2
For the isPalindrome method in Listing 18.3, what are the base cases? How many times is this method called when invoking isPalindrome("abdxcxdba")?
▼14.4.3
Show the call stack for isPalindrome("abcba") using the method defined in Listing 18.3.
Section 14.5
▼14.5.1
Show the call stack for isPalindrome("abcba") using the method defined in Listing 18.4.
▼14.5.2
Show the call stack for selectionSort(new double[]{2, 3, 5, 1}) using the method defined in Listing 18.5.
▼14.5.3
What is a recursive helper method?
Section 14.6
▼14.6.1
What is the base case for the getSize method?
▼14.6.2
How does the program get all files and directories under a given directory?
▼14.6.3
How many times will the getSize method be invoked for a directory if the directory has three subdirectories and each subdirectory has four files?
▼14.6.4
Will the program work if the directory is empty (i.e., it does not contain any files)?
▼14.6.5
Will the program work if line 20 is replaced by the following code?
for (int i = 0; i < files.length; i++)
▼14.6.6
Will the program work if lines 20-21 is replaced by the following code?
for (File file: files) size += getSize(file); // Recursive call
Section 14.7
▼14.7.1
How many times is the moveDisks method in Listing 18.8 invoked for moveDisks(5, 'A', 'B', 'C')?
Section 14.9
▼14.9.1
Which of the following statements are true?
a. Any recursive method can be converted into a nonrecursive method.
b. Recursive methods take more time and memory to execute than nonrecursive methods.
c. Recursive methods are always simpler than nonrecursive methods.
d. There is always a selection statement in a recursive method to check whether a base case is reached.
a. Any recursive method can be converted into a nonrecursive method.
b. Recursive methods take more time and memory to execute than nonrecursive methods.
c. Recursive methods are always simpler than nonrecursive methods.
d. There is always a selection statement in a recursive method to check whether a base case is reached.
▼14.9.2
What is a cause for a stack-overflow exception?
Section 14.10
▼14.10.1
Identify tail-recursive methods in this chapter.
▼14.10.2
Rewrite the fib method in Listing 18.2 using tail recursion.