chapter14_6e

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

TRUE/FALSE

1.There can be more than one stopping case in a recursive function.

ANSWER: TRUE

2.You may have at most 1 recursive call in a recursive function.

ANSWER: FALSE

3.Recursive functions must return a value.

ANSWER: FALSE

4.The operating system uses a stack to control recursion.

ANSWER: TRUE

5. A class member function may be recursive.

ANSWER: TRUE

6.Recursive functions may return any type of value

ANSWER: TRUE

7.Not all recursive definitions may be written iteratively.

ANSWER: FALSE

8.Recursive functions always execute faster than an iterative function.

ANSWER: FALSE

9.Only functions that do not return a value may be recursive.

ANSWER: FALSE

10.Every recursive definition may be rewritten iteratively.

ANSWER: TRUE

Short Answer

1. A recursive function is a function that ______________.

ANSWER: calls itself

2. A stack exhibits what behavior?

ANSWER: Last In – First Out

3.In a recursive function, the statement(s) that invoke the function again are called

the ______________.

ANSWER: recursive calls

4.If the recursive function call does not lead towards a stopping case, you have

______________.

ANSWER: infinite recursion or a stack overflow

5.How do you ensure that your function does not have infinite recursion?

ANSWER: All recursive calls must lead to a stopping case.

6.What is the output of the following code fragment?

int f1(int base, int limit)

{

if(base > limit)

return -1;

else

if(base == limit)

return 1;

else

return base * f1(base+2, limit);

}

int main()

{

cout << f1(2,4)<

return 0;

}

ANSWER: 2

7.Given the following code fragment, what is the stopping condition(s)?

int f1(int x, int y)

{

if(x<0 || y<0)

return x-y;

else

return f1(x-1,y) + f1(x,y-1);

}

int main()

{

cout << f1(1,2)<

return 0;

}

ANSWER: x<0, y<0

8.In a recursive power function that calculates some base to a positive exp power, at

what value of exp do you stop? The function will continually multiply the base

times the value returned by the power function with the base argument one

smaller.

ANSWER: exp=1

9.In a recursive power function that calculates some base to the exp power, what is

the recursive call?

ANSWER: return base * power(base,exp-1);

10.For every recursive solution, there is a(n) ______________ solution.

ANSWER: iterative

Multiple Choice

1. A definition that defines a concept or a formula in terms of the concept or formula

is called

a. a recursive definition

b.indecision

c.iteration

d.reduction

ANSWER: A

2.If you try to solve a problem recursively, you should

a.find all the stopping cases and the values if needed at that case

b.find a recursive call that will lead towards one of the stopping cases

c.all of the above

d.none of the above

ANSWER: C

相关文档
最新文档