Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Full file at https://fratstock.

eu

ch2

True/False
Indicate whether the statement is true or false.

____ 1. Recursion is a repetitive process in which an algorithm calls itself.

____ 2. The general case for the factorial recursive algorithm is: n * factorial (n + 1).

____ 3. Every recursive algorithm must have a base case.

____ 4. Every recursive call must either solve a part of the problem or reduce the size of the problem.

____ 5. In a recursive algorithm, the rest of the algorithm (the part that is not the base case) is known as the
general case.

____ 6. Recursion works best when the algorithm uses a data structure that naturally supports recursion.

____ 7. An interactive user interface program is well suited for recursive programming.

____ 8. A recursive solution to a problem is advisable, even when the equivalent iterative solution runs much
faster.

____ 9. A nonrecursive algorithm generally runs more slowly than its recursive implementation.

____ 10. If the recursion is deep we may run out of memory.

____ 11. All looping algorithms can and should be implemented with recursion.

____ 12. It is advisable to write a recursive algorithm even if the recursive solution is longer and more complex
than the iterative one.

____ 13. When the recursion occurs at the end of the function it is known as tail recursive.

____ 14. When the recursion occurs at the end of a function, an optimized compiler turns the recursive code into a
simple loop, thus eliminating the function call inefficiency.

____ 15. Recursive solutions may involve extensive overhead (both time and memory) because they use calls.

____ 16. “0, 1, 2, 3, 5, 8, 13, 21, 34” are the first few numbers of the Fibonacci series.

____ 17. A recursive solution to calculate Fibonacci numbers is efficient even for large numbers.

____ 18. The basic format of the algebraic notation we usually employ in every day life is called prefix notation.

____ 19. In prefix notation, the operator comes after its two operands.

____ 20. Postfix notation is also known as reverse Polish notation (RPN).
Full file at https://fratstock.eu

____ 21. When converting from prefix to postfix, we begin parsing at the left and work right until we isolate one
binary expression.

____ 22. A recursive solution is usually much harder to code than the iterative solution.

____ 23. The postfix representation of the prefix expression “-+*ABC/EF” is “AB*C+EF/-”.

____ 24. The Towers of Hanoi problem is best solved with a recursive algorithm.

____ 25. The base case of the Towers of Hanoi recursive algorithm is to move one disk from source to destination.

Multiple Choice
Identify the choice that best completes the statement or answers the question.

____ 26. What will be the value of n the third time recursive factorial is called? The original (first) call is
“factorial(5)”.
Algorithm recursiveFactorial (n)
if (n equals 0)
return 1
else
return (n * recursiveFactorial (n - 1))
end if
end recursiveFactorial
a. 1 c. 3
b. 2 d. 4
____ 27. What is the postcondition of the following recursive algorithm?
Algorithm recursiveAlgorithm (n)
if (n equals 0)
return 1
else
return (n * recursiveAlgorithm (n - 1))
end if
end recursiveAlgorithm
a. n is returned c. n! is returned
b. n^2 is returned d. sum(1..n) is returned
____ 28. In recursion, the statement that “solves” the problem is known as the _____.
a. return statement c. recursive call
b. base case d. recursive step
____ 29. The _____ contains the logic needed to reduce the size of the problem.
a. parameter list c. base case
b. return statement d. general case
____ 30. Every recursive call must either solve a part of the problem or _____.
a. reduce the size of the problem
b. increase the size of the problem
c. call itself again
d. check if a base case has been reached
Full file at https://fratstock.eu

____ 31. Which of the following problems is most suitable to be solved using recursion?
a. binary search c. displaying a text menu to the user
b. initializing an array with zeros (0) d. randomly shuffling a deck of cards
____ 32. As a general rule, recursive algorithms should be used only when their efficiency is _____.
a. linear c. quadratic
b. logarithmic d. exponential
____ 33. The recursion is said to be deep when _____.
a. there are few recursive calls c. its efficiency is exponential
b. its efficiency is quadratic d. there are many recursive calls
____ 34. _____ are naturally recursive structures.
a. Trees c. Abstract data types
b. Scalar data items d. Arrays
____ 35. Consider the following algorithm that calculates greatest common divisor using the Euclidean algorithm:
Algorithm gcd (a, b)
Post greatest common divisor returned
if (b equals 0)
return a
end if
if (a equals 0)
return b
end if
return gcd (b, a mod b)
end gcd
What should be its pre-condition?
a. a and b are positive integers greater than 0
b. a and b are positive integers, and a > b
c. a and b are positive integers, and a < b
d. a and b are integers
____ 36. The following algorithm that calculates greatest common divisor using the Euclidean algorithm has _____
base case(s).
Algorithm gcd (a, b)
Post greatest common divisor returned
if (b equals 0)
return a
end if
if (a equals 0)
return b
end if
return gcd (b, a mod b)
end gcd
a. 0 c. 2
b. 1 d. 3
____ 37. Consider the following function to calculate the nth Fibonacci number:
long fib (long num)
{
if (num == 0 || num == 1)
Full file at https://fratstock.eu

return num;
return (fib (num - 1) + fib (num - 2));
}
What is(are) the base case(s)?
a. 0 c. 0 and 1
b. 1 d. num - 1
____ 38. Consider the following function to calculate the nth Fibonacci number:
long fib (long num)
{
if (num == 0 || num == 1)
return num;
return (fib (num - 1) + fib (num - 2));
}
What line of code represents the general case?
a. long fib (long num)
b. if (num == 0 || num == 1)
c. return num;
d. return (fib (num - 1) + fib (num - 2));
____ 39. The following function to calculate the nth Fibonacci number is inefficient because _____.
long fib (long num)
{
if (num == 0 || num == 1)
return num;
return (fib (num - 1) + fib (num - 2));
}
a. it tests both base cases in the same line
b. it is tail recursive
c. it does not check if num is negative
d. it calculates each fib(n) twice: when called with fib(n+2) and when called with fib(n+1)
____ 40. In _____ notation the operator comes between the operands.
a. prefix c. postfix
b. infix d. rpn
____ 41. The expression “*AB” is in _____.
a. prefix notation c. postfix notation
b. infix notation d. rpn
____ 42. In designing a recursive algorithm to convert prefix expressions to the postfix format, the base case is
_____.
a. finding an operator c. running out of characters
b. finding an operand d. finding an end-of-line character
____ 43. From the recursive problems listed below, in which after each base case, do we return to a decomposition
of the general case for several steps?
a. nth Fibonacci number c. n!
b. binary search d. Towers of Hanoi
____ 44. What does the following recursive algorithm do?
Algorithm recursiveAlgorithm(n)
if (n equals 0)
Full file at https://fratstock.eu

return 0
end if
return recursiveAlgorithm(n-1)
end recursiveAlgorithm
a. Returns the sum of the numbers from 1 to n
b. Returns n!
c. Returns n
d. Returns 0
____ 45. What is wrong with the following recursive algorithm?
Algorithm recursiveAlgorithm(n)
if (n equals 0)
return 0
end if
return n + recursiveAlgorithm(n)
end recursiveAlgorithm
a. It does not have a base case
b. The general case does not reduce the size of the problem
c. The base case should be 1
d. It should not have a base case
____ 46. What is wrong with the following recursive algorithm?
Algorithm recursiveAlgorithm(n)
if (n equals 0)
return recursiveAlgorithm(1)
end if
return n + recursiveAlgorithm(n-1)
end recursiveAlgorithm
a. The base case calls the recursive algorithm
b. The base case should be 1
c. The general case does not reduce the size of the problem
d. It should not have a base case
____ 47. What is the 7th Fibonacci number? (Hint: the 0th is 0 and the 1st is 1)
a. 5 c. 13
b. 8 d. 21
____ 48. Consider the following function to calculate the nth Fibonacci number:
long fib (long num)
{
if (num == 0 || num == 1)
return num;
return (fib (num - 1) + fib (num - 2));
}
How many calls does it take to determine fib(4)?
a. 5 c. 15
b. 9 d. 25
____ 49. In designing a recursive algorithm to convert prefix expressions to the postfix format, the base case for the
algorithm that determines the length of the prefix expression is _____.
a. finding an operator c. running out of characters
b. finding an operand d. finding an end of line character
Full file at https://fratstock.eu

____ 50. The solution for the Towers of Hanoi problem requires a recursive algorithm with four parameters: _____,
the source needle, the destination needle, and the auxiliary needle.
a. the position of the disk to move
b. the number of times the recursion algorithm has been called
c. the number of disks moved so far
d. the number of disks to be moved
Full file at https://fratstock.eu

ch2
Answer Section

TRUE/FALSE

1. ANS: T PTS: 1 REF: 47


2. ANS: F PTS: 1 REF: 49
3. ANS: T PTS: 1 REF: 49
4. ANS: T PTS: 1 REF: 49
5. ANS: T PTS: 1 REF: 49
6. ANS: T PTS: 1 REF: 50
7. ANS: F PTS: 1 REF: 50
8. ANS: F PTS: 1 REF: 50
9. ANS: F PTS: 1 REF: 50
10. ANS: T PTS: 1 REF: 50
11. ANS: F PTS: 1 REF: 50
12. ANS: F PTS: 1 REF: 50
13. ANS: T PTS: 1 REF: 50
14. ANS: T PTS: 1 REF: 50
15. ANS: T PTS: 1 REF: 50
16. ANS: F PTS: 1 REF: 54
17. ANS: F PTS: 1 REF: 57
18. ANS: F PTS: 1 REF: 57-58
19. ANS: F PTS: 1 REF: 58
20. ANS: T PTS: 1 REF: 58
21. ANS: T PTS: 1 REF: 59
22. ANS: F PTS: 1 REF: 65
23. ANS: T PTS: 1 REF: 63
24. ANS: T PTS: 1 REF: 65
25. ANS: T PTS: 1 REF: 67

MULTIPLE CHOICE

26. ANS: C PTS: 1 REF: 48


27. ANS: C PTS: 1 REF: 47
28. ANS: B PTS: 1 REF: 49
29. ANS: D PTS: 1 REF: 49
30. ANS: A PTS: 1 REF: 49
31. ANS: A PTS: 1 REF: 50
32. ANS: B PTS: 1 REF: 50
33. ANS: D PTS: 1 REF: 50
34. ANS: A PTS: 1 REF: 50
35. ANS: A PTS: 1 REF: 53
36. ANS: C PTS: 1 REF: 53
37. ANS: C PTS: 1 REF: 56-57
38. ANS: D PTS: 1 REF: 56-57
Full file at https://fratstock.eu

39. ANS: D PTS: 1 REF: 56-57


40. ANS: B PTS: 1 REF: 58
41. ANS: A PTS: 1 REF: 58
42. ANS: B PTS: 1 REF: 59
43. ANS: D PTS: 1 REF: 65
44. ANS: D PTS: 1 REF: 47-48
45. ANS: B PTS: 1 REF: 49
46. ANS: A PTS: 1 REF: 49
47. ANS: C PTS: 1 REF: 54
48. ANS: B PTS: 1 REF: 57
49. ANS: B PTS: 1 REF: 60
50. ANS: D PTS: 1 REF: 68

You might also like