Recursion Pre Test

 

Knowledge/Memory:

1.      Recursion is:

a)      One method calling another method more than once.

b)      The ability of a function to call itself.

c)      The process of calling a method iteratively until a certain condition is met.

d)      None of the above.

Answer: B

2.      Which of the following statements is true?

 

a.       It is possible to have more than one end case in a recursive method.

b.      The end case is intended to prevent the method from calling itself an infinite number of times.

c.       Both of the above are true.

d.      None of the above is true.

Answer: C

3.      Recursion is always more efficient than iteration. True or false?

 

a.       True

b.      False

Answer: B

4.      If a recursive method is the most natural way to express a solution, then it is always the best way to an express a solution. True or false?

 

a.       True

b.      False

Answer: B

Comprehension:

 

5.      You have an iterative Java method, calculate(). When is it possible to write a recursive version of calculate()?

 

a.       It is always possible to write a recursive version of calculate().

b.      It is generally possible to write a recursive version of calculate().

c.       It is rarely possible to write a recursive version of calculate().

d.      It is never possible to write a recursive version of calculate().

 

Answer: A

 

 

Application:

6.      Given the definition of the function, mystery():

               mystery(0,Q) = Q

               mystery(P,Q) = mystery(P-1, Q+1)

     What is the result of mystery(2,4)?

a)      0

b)      2

c)      4

d)      6

Answer: D

7.      Given the definition of the function, mystery():

               mystery(0,Q) = Q

               mystery(P,Q) = mystery(P-1, Q+1)

Which of the following code segments correctly implements mystery()?

      a)      int mystery( int P, int Q )
               {
                if ( Q==0 )
                {
                  return Q;
                 }
                 else
                {
                return mystery(P-1, Q-1);  
                }
               }
 
b)      int mystery( int P, int Q )
               {
               if ( P==Q )
               {
               return Q;
               }
               else
               {
               return mystery(P-1, Q);  
               }
               }
 
c)      int mystery( int P, int Q )
               {
               if ( P==0 && Q==0)
               {
                return 1;
               }
               else
               {
               return mystery(P-1, Q+1);  
               }
               }
 
d)      int mystery( int P, int Q )
               {
               if ( P==0 )
               {
               return Q;
               }
               else
               {
               return mystery(P-1, Q+1);  
               }
               }

Answer: D

 

8.      Given the definition of the function, compute():

               compute(1) = 1

               compute(N) = compute(N-1) + 2*N -1

Which of the following code segments correctly implements compute()?

a)       int compute( int N )
               {
                if ( N<1 )
                 {
                   return 1;
                 }
                else
                 {
                  return N*N;
               }
 
b)       int compute( int N )
               {
                 if ( N==1 )
                 {
                   return 1;
                 }
               else
                {
                  return compute(N-1) + 2*N - 1;  
                }
               }
 
c)       int compute( int N )
               {
                 if ( N != 1 ) 
                 {
                   return 1;
                 }
                 else
                 {
                return compute(N-1) + 2*N - 1;  
                 }
            }
 
d)       int compute( int N )
               {
                if ( N==1 )
                {
                   return 1;
                 }
                 else
                 {
                   return compute(N);  
                 }
               }
 
Answer: B

9.      Select the recursive function that defines the sequence 5, 8, 11, 14, 17, 20.

a)      s(0) = 5   ;  s(n) = n + 3

b)      s(0) = 5   ;  s(n) = s(n-1)*2 - 2

c)      s(n) = 3 * n + 5
d)      s(0) = 5   ;  s(n) = 3 + s(n-1)
       

Answer: D

10.  Given the recursive definition, div(), for integers N and M:

               div( N, M ) = 0, if N < M
               div( N, M ) = 1 + div( N-M, M ), if N >= M

What is the result of div(17, 5 ) ?

a)      1

b)      2

c)      3

d)      5

Answer: C