IS 247 Chapter 18 - RECURSION

¡Supera tus tareas y exámenes ahora con Quizwiz!

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); } } }

(a) Sum is 15 (5 + 4 + 3 + 2 + 1 = 15) (b) 7654321

What are the three characteristics fof recursion?

1) The method is implemented using an if-else or switch statement that leads to different cases 2) One or more base cases (the simplest case) are used to stop recursion. 3) Every recursive call reduces the original problem, bringing it increasing closer to a base case until it becomes that case.

What is the output of the following code? public class Test { public static void main(String[] args) { System.out.println(m(4)); } public static int m(int r) { return r > 2 ? r * m(r - 1) : r; } }

24

What is the return value for xMethod(4) after calling the following method? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } A. 12 B. 11 C. 10 D. 9

C 4+3+2+1 = 10

Fill in the code to complete the following method for computing factorial. /** Return the factorial for a specified index */ public static long factorial(int n) { if (n == 0) // Base case return 1; else return _____________; // Recursive call } A. n * (n - 1) B. n C. n * factorial(n - 1) D. factorial(n - 1) * n

CD "factorial" must be a part of your answer because it is a recursive call!

How many times is the factorial method in Listing 18.1 invoked for factorial(5)? Listing 18.1: public static long factorial(int n) { if (n==0) // Base case return 1; else return n * factorial (n-1); A. 3 B. 4 C. 5 D. 6

D

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 + " "); } } }

(a) The output is 5 4 3 2 1 (b) The output is 1 2 3 4 5

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(); } }

(a) n is double. There is no guarantee that n != 0 will be eventually false. (b) Infinite recursion due to new Test() inside the constructor Test().

What is the output of the following code? public class Test { public static void main(String[] args) { m(4); } public static void m(int r) { if (r > 2) { System.out.print(r + " "); m(r - 1); } } }

4 3

What is the output of the following code? public class Test { public static void main(String[] args) { m(4); } public static void m(int r) { if (r >= 2) { System.out.print(r + " "); m(r - 1); } } }

4 3 2

Fill in the code to complete the following method for checking whether a string is a palindrome. public static boolean isPalindrome(String s) { if (s.length() <= 1) // Base case return true; else if _____________________________ return false; else return isPalindrome(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

What is a recursive method? What is an infinite recursion?

A recursive method is the one that calls itself. An infinite recursion is the one that never stops.

Which of the following statements are true? A. Every recursive method must have a base case or a stopping condition. B. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. C. Infinite recursion can occur if recursion does not reduce the problem in a manner that allows it to eventually converge into the base case. D. Every recursive method must have a return value. E. A recursive method is invoked differently from a non-recursive method.

ABC

What are the base cases in the following recursive method? public static void xMethod(int n) { 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 You are dividing so the number will keep getting smaller and smaller. the if condition already accounts for positive values so the else must be everything else.

Analyze the following recursive method. public static long factorial(int n) { return n * 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 It is expected to return type, long.

What stops recursion ?

One or more base cases (the simplest case) are used to stop recursion

T/F: Every recursive call reduces the original problem, until it BECOMES that case.

T

Analyze the following code: public class Test { public static void main(String[] args) { int[] x = {1, 2, 3, 4, 5}; xMethod(x, 5); } public static void xMethod(int[] x, int length) { 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.

The correct answer is D Explanation: xMethod(x, 5) is invoked, then xMethod(x, 4), xMethod(x, 3), xMethod(x, 2), xMethod(x, 1), xMethod(x, 0). When invoking xMethod(x, 0), a runtime exception is raised because System.out.print(' '+x[0-1]) causes array out of bound.

In the following method, what is the base case? static int xMethod(int n) { if (n == 1) return 1; else return n + xMethod(n - 1); } A. n is 1. B. n is greater than 1. C. n is less than 1. D. no base case.

a

Write a recursive mathematical definition for computing 1 + 2 + 3 + ... + n for a positive integer n.

f(n) = 1 if n = 1 f(n) = f(n-1) + n for (n > 1)

Write a recursive mathematical definition for computing 2 n for a positive integer n.

f(n) = 2 if n = 1 f(n) = 2 * 2^(n-1) for (n > 1)

Write a recursive mathematical definition for computing x n for a positive integer n and a real number x.

f(n) = x if n = 1 f(n) = x * x^(n-1) for (n > 1)


Conjuntos de estudio relacionados

Vertex of Quadratics in Standard Form

View Set

Business Communication final exam study guide.

View Set

APUSH terms: Franklin, Benjamin-Fugitive Slave Act

View Set

Tableau Essential Training—LinkedIn

View Set