Quiz 07 sample questions

The quiz will have two questions, and cover material on recursion.


In the first question, you will demonstrate your ability to read recursive methods. For example, a question might read:

In the following method, (i) identify base case(s), (ii) identify the recursive case(s), and (iii) state whether the method will always terminate normally, without throwing an exception. If (i) or (ii) are implicit, then note what they depend upon; or, if (i) or (ii) are missing, then note that fact.

static int fib(int n) {
    if (n == 0) {
        return 1;
    }
    else {
        return fib(n - 1) + fib(n - 2);
    }
}

Assume n > 0 when first invoked.


In the second questions, you will show you can write simple recursive methods given an textual description of their behavior. For example, a question might state:

Write a recursive definition (not the code) for contains on a linked list of Node<E>, starting at a given node, in the style from lecture.

Then, implement boolean contains(Node<Integer> node, int e) using recursion – no explicit loop constructs (for, while) allowed!

(Note that I expect you to be able use recursion to process either linked lists or arrays. You should also be capable of writing boolean contains(int[] array, int i, int value).)


You can check your answers here.