CPS 181-Chapter 15 Review

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A method is called from the main method for the first time. It then calls itself seven times. What is the depth of recursion?

7

A recursive method must have which of the following?

Some way to control the number of time it repeats

T/F A problem can be solved recursively if it can be broken down into successive smaller problems that are identical to the overall

True

T/F Any problem that can be solved recursively can also be solved iteratively, with a loop.

True

________ occurs when method A calls method B which in turn calls method A.

Indirect Recursion

A method that calls itself is a ________ method.

recursive

In the following code that uses recursion to find the greatest common divisor of a number, what is the base case? public static int gcd(int x, int y) { if (x % y == 0) return y; else return gcd(y, x % y); }

if (x % y ==) return y;

Given the following code that uses recursion to find the factorial of a number, how many times will the else clause be executed if n = 5? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }

5

The ________ is at least one case in which a problem can be solved without recursion.

Base case

When recursive methods directly call themselves, it is known as ________.

Direct recursion

T/F A recursive method can have only one base case.

False

T/F The recursive case does not require recursion, so it stops the chain of recursive calls.

False

T/F Without a base case, a recursive method will call itself only once and stop.

False

In the ________, the problem must be reduced to a smaller version of the original problem.

Recursive Case

In the following code that uses recursion to find the factorial of a number, what is the base case? private static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n - 1); }

if (n==0); return 1;


Kaugnay na mga set ng pag-aaral

Chapter 13: Analyzing Subnet Masks

View Set

MA 2: How Genetic Information is Organized

View Set

Chapter 57: Care of Patients with Inflammatory Intestinal Disorders

View Set

Texas Real Estate STANDARDS OF CONDUCT.

View Set

NUR 2420 Maternal Nursing Chapter 14: Nursing Management During Labor and Birth

View Set