Created 01/07/03

# on Recursion with Java

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 (for positive integers):

```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

6. Consider rem( a, d ) defined as follows (for positive integers):

```rem(a,d) = a if a < d
otherwise
rem(a,d) = rem(a-d, d)
```

According to this definition, what is `rem(17,4)`?

A.    `rem(17,4) = rem(17) + 4`

B.    `rem(17,4) = rem(13,4) + 4`

C.    `rem(17,4) = rem(13,4)`

D.    `rem(17,4) = rem(16,3)`

7. Look at `rem()` again:

```rem(a,d) = a if a < d
otherwise
rem(a,d) = rem(a-d, d)
```

Which Java method below successfully implements this definition?

A.

```int rem( int a, int d )
{
if ( a<d )
{
return 1;
}
else
{
return rem(a-d);
}
}
```

B.

```int rem( int a, int d )
{
if ( a<d )
{
return a;
}
else
{
return rem(a-d, d);
}
}
```

C.

```int rem( int a, int d )
{
if ( a<d )
{
return a;
}
else
{
return rem(a-d, a);
}
}
```

D.

```int rem( int a, int d )
{
if ( a<d )
{
return a-d;
}
else
{
return rem(a-d, d-a);
}
}
```

8. Look at `rem()` again:

```rem(a,d) = a if a < d
otherwise
rem(a,d) = rem(a-d, d)
```

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

A.    1

B.    3

C.    4

D.    5

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.