1. Recursion is simply when _______________________.
2. The following method demonstrates an example of:
3. Which line in the method takingTime (shown above) contains the recursive call (the call to the same method)?
4. Which of the following statements is true of the following code snippet?
5. The following code demonstrates recursion.
6. Recursion is most useful when it is used to solve problems where the structure of the problem repeats.
7. A common misconception is that Recursion can be used to create fractals. E.g Sierpinski's triangle in which you subdivide a triangle into 4 new triangles.
8. Recursion can also be used to traverse String, array, and ArrayList objects, much like a loop.
9. Any recursive solution could be written with iteration (loops) instead.
10. Which line in the method factorial contains the recursive call (the call to the same method)?
11. The above code does in fact NOT accurately calculate the factorial of a number. What needs to be changed for it to do so?
12. Analyse the following code carefully. What is the output?
public class MysteryTest
{
public static int mystery(int n)
{
if (n == 0)
return 2;
else
return n * mystery(n-1);
}
public static void main(String[] args)
{
System.out.println("The mysterious output is:" + mystery(3));
}
}
13. Every recursive method must have at least one ________ which halts the recursion.
14. Which line in the following code contains the test for the base case.
public static int factorial(int n)
{
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
15. What is the value of n when this method stops calling itself (when it reaches the base case)?
16. What is/are the values of the variable eggs when this method stops calling itself (when it reaches the base case)?
17. The following code returns:
18. Given the method defined below what does the following print: mystery(4,3)?
19. Given the method defined below what does the following print: mystery(1234)?
20. This method traverses a String. Given the method defined below what does the following return: mystery('xyzxyxy')?
21. Analyse the code below. What is the value of A in the trace above?
22. Hard question: Given the following method declaration, what will redo(82, 3) return?
23. This method will return true when s has at least 2 characters in it and at least 2 characters are the same and are adjacent.
24. Which of the following is this excerpt/definition referring to?
25. The result from product(5) is 5 * product(3) which is 3 * product(1) which is 1 so the answer is 1 * 3 * 5 = 15.