DS Q3-1

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

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

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

Indirect Recursion

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

Recursive Case

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

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;

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 ==0) return y;

A method that calls itself is a ________ method.

recursive

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

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


Kaugnay na mga set ng pag-aaral

AWS - Cloud Practitioner Certification - Module 1

View Set

Anatomy and Physiology of Thalamus

View Set

Chapter 3: Competitive Intelligence

View Set

HESI Review Test-Maternity, Evolve Obstetrics/Maternity Practice 2017 Exam, HESI Maternity

View Set

Ocean Water, Chapter 20, Earth Science 7

View Set

Chapter 12: Principles of Test Selection and Administration

View Set

Foundations of Physical Fitness FINAL

View Set

Med surg final exam review practice questions FINAL HOLY SHIT THIS IS LONG

View Set

Ch. 11 Information and Data Management: Organizing, Verifying, Maintaining, and Accessing

View Set

BIOL 1031 Mastering Biology Chapter 50

View Set