Name:Score =
/ 25
Homework 8 – Recursion, Scope, and Exception Handling
Due Friday, March 19 at 6pm
1. If you are going to solve a problem using recursion, which of the following is true?
a. The recursive solution will be more efficient than the iterative solution.
b. The problem has a base case or base cases for which the answer can be gotten with no
further work.
c. The recursive solution will probably use loops to perform the repetition.
d. Both b and c.
2. Which of the following is true?
a. All problems can be solved recursively.
b. Some problems are easier to solve recursively rather than iteratively.
c. Recursion usually uses less memory than iterative solutions.
d. Recursion never uses for loops as part of its work.
3. Describe one advantage and one disadvantage of recursive solutions.
4. For the problem of computing a number raised to an integer power,
a. identify the decomposition step (1 point)
b. identify the base case (1 point)
c. Write the code for this in Python (3 points)
d. Draw the series of recursive steps this function will take to compute 3 raised to the 3rd
power. (2 points)
5. Listing the files on a hard drive, thumb drive, or folder is an example of a problem that can be
solved recursively. Write a recursive algorithm for a function that will take in a path and then list
all the files that can be reached from that path. (5 points)
6. Consider this code (3 points)
a. Where will the circle be drawn (i.e. at what (x,y) value)? Explain. (0.5 point)
b. What will the radius of the circle that is drawn? Explain. (0.5 point)
c. With what color will the circle be drawn? Explain. (0.5 point)
d. What is printed by the program at the end? Explain. (0.5 point)
e. What color will sam be at the end of this program? Explain. (1 point)
7. Given this binary tree, write the order of nodes that would be printed by in-order, pre-order,
and post-order traversal. (3 points)
Pre-order:
In-order
Post-order
8. Write a program that will take in a number and then let the user know if that number is even or
odd. Use exception handling and looping to keep asking the user to respond until they actually
enter an int. (4 points. 1 point for determining if the number is even or odd, 2 points for
trapping the exception, and 1 point for repeatedly asking the user until they comply.)