Chpt. 4

Ace your homework & exams now with Quizwiz!

As one method calls another which calls another, their corresponding activation records are stored A. on a stack B. in a queue C. in static memory D. on a disk

A

int example (int n) { if (n == 0) return 0; else return example (n - 1) + n*n*n; } What is the limiting condition of example? A. n >= 0 B. n > 0 C. n = 0 D. n < 0

A

int example (int n) { if (n == 0) return 0; else return example (n - 1) + n*n*n; } What is returned from the call example(3)? A. 0 B. 27 C. 29 D. 36 E. nothing, the method runs forever

D

What is the Smaller-Caller Question?

Does each recursive call to the algorithm involve a smaller case of the original problem, leading inescapably to the base case?

Static storage allocation by a compiler is used to support recursion.

False

True or False? A method that calls itself is called an iterative method.

False

True or False? A recursive solution to a problem is always better than an iterative solution.

False

True or False? All programming languages support recursion

False

True or False? Recursive algorithms are usually implemented with while loops.

False

True or False? Recursive methods are always shorter and clearer than the equivalent nonrecursive methods.

False

True or False? Recursive methods are possible only in languages with static storage allocation.

False

True or False? Recursive methods generally use while or for statements as their main control structure.

False

True or False? Recursive methods should be used whenever execution speed is critical.

False

True or False? The "general" case in recursion is the case that cannot be stated recursively.

False

True or False? The static storage allocation approach creates space for a method when the method is invoked.

False

Explain what is meant by an activation record.

Space used at run time to store information about a method call.

Define "recursive algorithm".

The algorithm solution is expressed in terms of smaller instances of itself, plus at least one base case.

int factorial (int n) { if (n > 0) return (n * factorial (n - 1)); else if (n == 0) return 1; } What are the constraints on the argument values?

The argument must not be less than zero.

int factorial (int n) { if (n > 0) return (n * factorial (n - 1)); else if (n == 0) return 1; } What is the base case?

The base case is when the argument is equal to zero.

Explain what is meant by tail recursion.

The case in which a method contains only a single recursive invocation and it is the last statement to be executed in the method.

int factorial (int n) { if (n > 0) return (n * factorial (n - 1)); else if (n == 0) return 1; } What is the general case?

The general case is when the argument is greater than zero.

True or False? A general case of a valid recursive algorithm must eventually reduce to a base case.

True

True or False? Recursive methods must always contain a path that does not contain a recursive call.

True

True or False? Recursive methods often have fewer local variables than the equivalent nonrecursive methods.

True

True or False? The base case does not exist or is not reached when there is infinite recursion.

True

True or False? The dynamic storage allocation approach creates space for a method when the method is invoked.

True

The case in which a method contains only a single recursive invocation and it is the last statement to be executed in the method is called ____________________________ .

tail recursion

What are the "names" of each of the three questions we use to help verify, design, and debug recursive solutions to problems?

the Base-Case Question, the Smaller-Caller Question, and the General-Case Question.

Explain what is meant by the run-time stack.

A system data structure that keeps track of activation records during execution of a program, i.e., the activation records are stored on the run-time stack.

What is the The General-Case Question?

Assuming the recursive call(s) to the smaller case(s) works correctly, does the algorithm work correctly for the general case?

int example (int n) { if (n == 0) return 0; else return example (n - 1) + n*n*n; } What does the example method do, when passed a positive int n? A. Returns the cube of the number n. B. Returns the sum of the cubes of the numbers 0 to n. C. Returns one less than n plus the cube of n. D. Runs forever (infinite recursion)

B

int example (int n) { if (n == 0) return 0; else return example (n - 1) + n*n*n; } How many base cases are there in example? A. 0 B. 1 C. 2 D. 3 E. More than 3

B

Describe the difference between direct and indirect recursion.

In direct recursion a method invokes itself, while in indirect recursion a method invokes another method, which in turn invokes the first method (the chain of calls can be longer but it eventually invokes the original method).

What is the Base-Case question (actually, it's two questions)?

Is there a non-recursive way out of the algorithm, and does the algorithm work correctly in this case?

int tq (int num) { if (num == 0) return 0; else if (num > 100) return -1; else return num + tq ( num - 1 ); } Is tq(-5) a valid call? If so, what is returned from the method?

No.

int factorial (int n) { if (n > 0) return (n * factorial (n - 1)); else if (n == 0) return 1; } What does the method do?

The method returns the factorial of the original argument; for example if passed 4 it returns 4! = 24.

Explain the relationship between dynamic storage allocation and recursion

With recursion a method can be invoked while some version of it is still "active" so dynamic storage allocation is needed.

int tq (int num) { if (num == 0) return 0; else if (num > 100) return -1; else return num + tq ( num - 1 ); } Is tq(250) a valid call? If so, what is returned from the method?

Yes, -1

int tq (int num) { if (num == 0) return 0; else if (num > 100) return -1; else return num + tq ( num - 1 ); } Is tq(0) a valid call? If so, what is returned from the method?

Yes, 0

int tq (int num) { if (num == 0) return 0; else if (num > 100) return -1; else return num + tq ( num - 1 ); } Is tq(4) a valid call? If so, what is returned from the method?

Yes, 10

int tq (int num) { if (num == 0) return 0; else if (num > 100) return -1; else return num + tq ( num - 1 ); } Is there a constraint on the value that can be passed as an argument in order for tq to pass the smaller-caller test? If so, what is it?

Yes, the value must not be less than 0.

Space used at run time to store information about a method call, including the parameters, local variables, and return address is called a(n) ________________________________ .

activation record or stack frame

The ___________________ storage allocation approach creates space for a method when the method is invoked.

dynamic

True or False? A recursive method should always have at least one base case.

true


Related study sets

Florida Mortgage Loan Officer Exam

View Set

Chapter 13 Fluid-Electrolytes: Balance and Disturbance Workbook

View Set

HESI Prep: Gastrointestinal System

View Set

(Marquis) Chapter 8 - How Organizations Work

View Set

Supply Chain Management Chapter 6: Strategic Sourcing

View Set

Chapter 3: How Data is Transported Over Networks

View Set

Chapter 12 - Recognizing Employee Contributions with Pay

View Set

Graphic Design and Illustration: Mastery Test

View Set