Created 01/07/03


on Examples of Recursion

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. Consider a definition of mystery():

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

According to this definition, what is mystery(2,4)?

A.    0
B.    2
C.    4
D.    6

2. Look at mystery() again:

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

Which of the following Java methods correctly implements it?

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);  
  }
}

3. Say that you have a recursive Java method, funct(). Is it always possible to write an iterative version of funct()?

A.    Yes.
B.    Usually, but not always.
C.    Almost never.
D.    No.

4. Say that you have an iterative Java method, foo(). Is it always possible to write a recursive version of foo()?

A.    Yes.
B.    Usually, but not always.
C.    Almost never.
D.    No.

5. Say that you have an recursive Java method, compute(). Is it always possible to write a method that implements compute() with a one line formula?

A.    Yes.
B.    Usually, but not always.
C.    Almost never.
D.    Never.

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


Click here

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.