Chapter 18: Recursion

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

E

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.

D

How many times is the factorial method in Listing 18.1 invoked for factorial(5)? A. 3 B. 4 C. 5 D. 6

B

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

How many times is the recursive moveDisks method invoked for 3 disks? A. 3 B. 7 C. 10 D. 14

C

How many times is the recursive moveDisks method invoked for 4 disks? A. 5 B. 10 C. 15 D. 20

A, B, and C

In LiveExample 18.9, to draw three smaller triangles recursively, the program invokes: A. displayTriangles(order - 1, p1, p12, p31); B. displayTriangles(order - 1, p12, p2, p23); C. displayTriangles(order - 1, p31, p23, p3); D. displayTriangles(order - 1, p12, p23, p31);

A

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.

B

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

C

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

A, B, and C

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.

B, C, and D

Which of the following statements are true? A. Recursive methods run faster than non-recursive methods. B. Recursive methods usually take more memory space than non-recursive methods. C. A recursive method can always be replaced by a non-recursive method. D. In some cases, however, using recursion enables you to give a natural, straightforward, simple solution to a program that would otherwise be difficult to solve.

A

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.


Ensembles d'études connexes

Spanish 2, Reciprocal reflexives, Lesson 11.3

View Set

1950's Questions - Modern History Exam

View Set

COM 203 Chapter 1 The Communication Process: Perception, Meaning, & Identity

View Set