Chapter 15 Flashcards

1
Q

True or false:
The following is a valid recursive definition to determine the factorial of a non-negative integer.
0! = 1
1! = 1
n! = n * (n - 1)! if n > 0

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or false:
In a recursive function, the base case stops the recursion.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

True or false:
With recursion, the base case must eventually be reduced to a general case.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

True or false:
The following is an example of a recursive function.
void print(int x)
{
if (x > 0)
{
cout &laquo_space;x &laquo_space;” “ ;
print (x - 1);
}
}

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

True or false:
Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

True or false:
Infinite recursions execute forever on a computer.

A

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or false:
ou can use a recursive algorithm to find the largest element in an array.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or false:
To design a recursive function, you must determine the limiting conditions.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A definition in which something is defined in terms of a smaller version of itself is called a(n) ____ definition.

A

recursive

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The ____ case is the case for which the solution to an equation is obtained directly.

A

base

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

int foo(int n)
{
if (n == 0)
return 0;
else
return n + foo(n - 1);
}
Consider the accompanying definition of a recursive function. Which of the statements represents the base case?

A

Statements in lines 3 and 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

int foo(int n)
{
if (n == 0)
return 0;
else
return n + foo(n - 1);
}
Consider the accompanying definition of a recursive function. Which of the statements represents the general case?

A

Statements in lines 5 and 6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

void printNum(int num)
{
if (n < 0)
cout &laquo_space;“Num is negative” &laquo_space;
endl;
else if (num == 0)
cout &laquo_space;“Num is zero” &laquo_space;endl;
else
{
cout &laquo_space;num &laquo_space;” “;
printNum(num – 1);
}
}
Consider the accompanying definition of a recursive function. Which of the statements represent the base case?

A

Statements in lines 3-6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

void printNum(int num)
{
if (n < 0)
cout &laquo_space;“Num is negative” &laquo_space;
endl;
else if (num == 0)
cout &laquo_space;“Num is zero” &laquo_space;endl;
else
{
cout &laquo_space;num &laquo_space;” “;
printNum(num – 1);
}
}
Consider the accompanying definition of a recursive function. Which of the statements represent the general case?

A

Statements in lines 7-11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

int recFunc(int num)
{
if (num >= 10)
return 10;
else
return num * recFunc(num + 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout &laquo_space;recFunc(8) &laquo_space;endl;

A

720

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

int recFunc(int num)
{
if (num >= 10)
return 10;
else
return num * recFunc(num + 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout &laquo_space;recFunc(10) &laquo_space;endl;

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Consider the following definition of the recursive function print.
void print(int num)
{
if (num > 0)
{
cout &laquo_space;num &laquo_space;” “;
print(num - 1);
}
}
What is the output of the following statement?
print(4);

A

4 3 2 1

18
Q

Tracing through ____ recursion is more tedious than tracing other recursive forms

A

indirect

19
Q

A function is called ____ if it calls itself.

A

directly recursive

20
Q

A recursive function in which the last statement executed is the recursive call is called a(n) ____ recursive function.

A

tail

21
Q

f every recursive call results in another recursive call, then the recursive function (algorithm) is said to have ____
recursion.

A

infinite

22
Q

int mystery(int list[], int first, int last)
{
if (first == last)
return list[first];
else
return list[first] + mystery(list, first + 1, last);
}
Consider the accompanying definition of the recursive function mystery. Given the declaration:
int alpha[5] = {1, 4, 5, 8, 9};
what is the output of the following statement?
cout &laquo_space;mystery(alpha, 0, 4) &laquo_space;endl;

A

27

23
Q

Consider the following definition of the recursive function mystery.
int mystery(int first, int last)
{
if (first > last)
return 0;
else if (first == last)
return first;
else
return first + mystery(first + 1, last - 1);
}
What is the output of the following statement?
cout &laquo_space;mystery(6, 10) &laquo_space;endl;

A

21

24
Q

Consider the following definition of the recursive function mystery.
int mystery(int num)
{
if (num <= 0)
return 0;
else if (num % 2 == 0)
return num + mystery(num – 1);
else
return num * mystery(num – 1);
}
What is the output of the following statement?
cout &laquo_space;mystery(5) &laquo_space;endl;

A

50

25
Q

int puzzle(int start, int end)
{
if (start > end)
return start - end;
else if (start == end)
return start + end;
else
return end * puzzle(start + 1, end - 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout &laquo_space;puzzle(5, 10) &laquo_space;endl;

A

720

26
Q

int puzzle(int start, int end)
{
if (start > end)
return start - end;
else if (start == end)
return start + end;
else
return end * puzzle(start + 1, end - 1);
}
Consider the accompanying definition of a recursive function. What is the output of the following statement?
cout &laquo_space;puzzle(3, 7) &laquo_space;endl;

A

42

27
Q

Which of the following function headings can be used for a recursive definition of a function to calculate the nth
Fibonacci number?

A

int rFibNum(int a, int b, int n)

28
Q

How many needles are used in the Tower of Hanoi problem?

A

three

29
Q

Which of the following rules should you follow to solve the Tower of Hanoi problem?

A

A smaller disk can be placed on top of a larger disk.

30
Q

____ control structures use a looping structure, such as while, for, or do…while, to repeat a set of statements.

A

iterative

31
Q

Which of the following solution methods would be the best choice for a mission control system?

A

recursive

32
Q

Which of the following solutions is easier to construct for the Tower of Hanoi problem?

A

recursive

33
Q

Consider the following recursive definition, where n is a positive integer.
F(1) = 3
F(n) = F(n - 1) + 1 if n > 1
The value of F(3) is _____.

A

5

34
Q

The recursive algorithm must have one or more base cases, and the general solution must eventually be reduced to
a(n) ___________

A

base case

35
Q

Recursive algorithms are implemented using ____________________ functions

A

recursive

36
Q

Consider the following code.
int fact(int num)
{
if (num == 0)
return 1;
else
return num * fact(num - 1);
}
The function fact is an example of a(n) ____________________ recursive function

A

tail

37
Q

Suppose that function A calls function B, function B calls function C, function C calls function D, and function D calls
function A. Function A is then ____________________ recursive

A

indirectly

38
Q

If a function A calls a function B and function B calls function A, then function A is ________________
recursive

A

indirectly

39
Q

If you execute an infinite recursive function on a computer, the function executes until the system runs out of _________

A

memory

40
Q

The _________ Fibonacci number in a sequence is the sum of the second and third Fibonacci numbers

A

fourth

41
Q

In the Tower of Hanoi problem, if needle 1 contains three disks, then the number of moves required to move all three
disks from needle 1 to needle 3 is ____.

A

7