Chapter 15
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 << mystery(5) << endl; a. 50 b. 65 c. 120 d. 180
a
Which of the following solution methods would be the best choice for a mission control system? a. Iterative b. Direct recursive c. Indirect recursive d. Infinite recursive
a
Which of the following solutions is easier to construct for the Tower of Hanoi problem? a. Recursive b. Iterative c. Procedural d. Step-by-step
a
____ control structures use a looping structure, such as while, for, or do...while, to repeat a set of statements. a. Iterative b. Recursive c. Procedural d. Object
a
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); } 28. Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << puzzle(5, 10) << endl; a. 720 b. 5040 c. 5760 d. 10800
a
int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } 18. Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << recFunc(10) << endl; a. 10 b. 11 c. 100 d. 110
a
A definition in which something is defined in terms of a smaller version of itself is called a(n) ____ definition. a. step-wise b. recursive c. member-wise d. iterative
b
A recursive function in which the last statement executed is the recursive call is called a(n) ____ recursive function. a. direct b. tail c. indefinite d. indirect
b
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 << mystery(6, 10) << endl; a. 13 b. 21 c. 40 d. 42
b
The ____ case is the case for which the solution to an equation is obtained directly. a. general b. base c. direct d. tail
b
Tracing through ____ recursion is more tedious than tracing other recursive forms. a. direct b. indirect c. tail d. iterative
b
int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } //Line 7 13. Consider the accompanying definition of a recursive function. Which of the statements represents the base case? a. Statements in Lines 1-6. b. Statements in Lines 3 and 4. c. Statements in Lines 5 and 6. d. Statements in Lines 3, 4, and 5.
b
A function is called ____ if it calls itself. a. directly iterative b. indirectly iterative c. directly recursive d. indirectly recursive
c
Consider the following definition of the recursive function print. void print(int num) { if (num > 0) { cout << num << " "; print(num - 1); } } What is the output of the following statement? print(4); a. 0 1 2 3 4 b. 1 2 3 4 c. 4 3 2 1 d. 4 3 2 1 0
c
How many needles are used in the Tower of Hanoi problem? a. one b. two c. three d. four
c
If every recursive call results in another recursive call, then the recursive function (algorithm) is said to have ____ recursion. a. unlimited b. indefinite c. infinite d. tail
c
int mystery(int list[], int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } 24. 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 << mystery(alpha, 0, 4) << endl; a. 1 b. 18 c. 27 d. 35
c
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); } 29. Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << puzzle(3, 7) << endl; a. 10 b. 21 c. 42 d. 420
c
void printNum(int num) //Line 1 { //Line 2 if (n < 0) //Line 3 cout << "Num is negative" << endl; //Line 4 else if (num == 0) //Line 5 cout << "Num is zero" << endl; //Line 6 else //Line 7 { //Line 8 cout << num << " "; //Line 9 printNum(num - 1); //Line 10 } //Line 11 } //Line 12 15. Consider the accompanying definition of a recursive function. Which of the statements represent the base case? a. Statements in Lines 3 and 4 b. Statements in Lines 5 and 6 c. Statements in Lines 3-6 d. Statements in Lines 5-10
c
Which of the following function headings can be used for a recursive definition of a function to calculate the nth Fibonacci number? a. void rFibNum(int a, int b) b. bool rFibNum(int a, int b) c. bool rFibNum(int a, int b, int n) d. int rFibNum(int a, int b, int n)
d
Which of the following rules should you follow to solve the Tower of Hanoi problem? a. Only two disks can be moved at a time. b. You can remove disks only from the first needle. c. The removed disk must be placed on a smaller disk. d. A smaller disk can be placed on top of a larger disk.
d
int foo(int n) //Line 1 { //Line 2 if (n == 0) //Line 3 return 0; //Line 4 else //Line 5 return n + foo(n - 1); //Line 6 } //Line 7 14. Consider the accompanying definition of a recursive function. Which of the statements represent the general case? a. Statements in Lines 1-6 b. Statements in Lines 3 and 4 c. Statements in Lines 4, 5, and 6 d. Statements in Lines 5 and 6
d
int mystery(int list[], int first, int last) { if (first == last) return list[first]; else return list[first] + mystery(list, first + 1, last); } 25. Consider the accompanying definition of the recursive function mystery. Given the declaration: int beta[10] = {2, 5, 8, 9, 13, 15, 18, 20, 23, 25}; What is the output of the following statement? cout << mystery(beta, 4, 7) << endl; a. 27 b. 33 c. 55 d. 66
d
int recFunc(int num) { if (num >= 10) return 10; else return num * recFunc(num + 1); } 17. Consider the accompanying definition of a recursive function. What is the output of the following statement? cout << recFunc(8) << endl; a. 4 b. 8 c. 72 d. 720
d
void printNum(int num) //Line 1 { //Line 2 if (n < 0) //Line 3 cout << "Num is negative" << endl; //Line 4 else if (num == 0) //Line 5 cout << "Num is zero" << endl; //Line 6 else //Line 7 { //Line 8 cout << num << " "; //Line 9 printNum(num - 1); //Line 10 } //Line 11 } //Line 12 16. Consider the accompanying definition of a recursive function. Which of the statements represent the general case? a. Statements in Lines 3-11 b. Statements in Lines 5-6 c. Statements in Lines 5-11 d. Statements in Lines 7-11
d
In the Tower of Hanoi recursive program, if needle 1 contains three disks, then the number of moves required to move all three disks from needle 1 to needle 3 is 8.
false
Infinite recursions execute forever on a computer.
false
The following is an example of a recursive function, where nextNum is a function such that nextNum(x) = x + 1. int recFunc(int x) { return nextNum(nextNum(x)); }
false
With recursion, the base case must eventually be reduced to a general case.
false
Every call to a recursive function requires the system to allocate memory for the local variables and formal parameters.
true
In a recursive function, the base case stops the recursion.
true
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
true
The following is an example of a recursive function. void print(int x) { if (x > 0) { cout << x << " " ; print (x - 1); } }
true
To design a recursive function, you must determine the limiting conditions
true
You can use a recursive algorithm to find the largest element in an array.
true