Created 01/07/03

# on Recursion with Java

This is a practice quiz. The results are not recorded anywhere and do not affect your grade. The questions on this quiz might not appear in any quiz or test that does count toward your grade.

Instructions: For each question, choose the single best answer. Make your choice by clicking on its button. You can change your answers at any time. When the quiz is graded, the correct answers will appear in the box after each question.

1. Which answer is a correct skeleton for a recursive Java method?

 A.    ```int solution( int N ) { if ( base case ) { return something easily computed } else { divide problem into pieces return something calculated from the solution to each piece } } ``` B.    ```int solution( int N ) { if ( base case ) { return something easily computed } else { return solution(N) } } ``` C.    ```int solution( int N ) { divide problem into pieces return something calculated from the solution to each piece } ``` D.    ```int solution( int N ) { divide problem into pieces if ( base case ) { return something easily computed } else { return something calculated from the solution to each piece } } ```

2. Consider square numbers defined as follows:

```square(1) = 1
square(N) = square(N-1) + 2N -1
```

According to this definition, what is `square(3)`?

 A.    `square(3) = square(2) + square(1)` B.    `square(3) = square(2) - 2*3 +1` C.    `square(3) = square(2) + 2*3 -1` D.    `square(3) = square(3) + 2*3 -1`

3. Look at square numbers again:

```square(1) = 1
square(N) = square(N-1) + 2N -1
```

Which Java method below successfully implements this definition?

 A.    ```int square( int N ) { if ( N<1 ) { return 1; } else { return N*N; } ``` B.    ```int square( int N ) { if ( N==1 ) { return 1; } else { return square(N-1) + 2*N - 1; } } ``` C.    ```int square( int N ) { if ( N=1 ) { return 1; } else { return square(N-1) + 2*N - 1; } } ``` D.    ```int square( int N ) { if ( N==1 ) { return 1; } else { return square(N); } } ```

4. Look at square numbers one more time:

```square(1) = 1
square(N) = square(N-1) + 2N -1
```

Assume the definition has been implemented correctly. How many activations will there be on the activation chain if `main()` calls `square(5)`?

 A.    1 B.    3 C.    5 D.    6

5. What are two ways to view recursion?

 A.    (i) static view, and (ii) dynamic view. B.    (i) recursive view, and (ii) iterative view. C.    (i) math view, and (ii) programming view D.    (i) code view, and (ii) translation view

The number you got right:       Percent Correct:       Letter Grade:

If you have returned here from another page, or have re-loaded this page, you will need to click again on each of your choices for the grading program to work correctly. You may want to press the SHIFT KEY while clicking to clear the old answers.