Created 01/07/03


Quiz on More Recursion

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. Here is another recursive definition involving integers N and M. Assume 0<=N and 0<M.

mod( N, M ) = N, if N < M
mod( N, M ) = mod( N-M, M ), if N >= M

What is the value of mod(2, 5)?

A.    2

B.    5

C.    7

D.    17


2. What is the value of mod(17, 5)?

A.    2

B.    5

C.    7

D.    17


3. Here (again) is the definition of mod() for integers N and M. Assume 0<=N and 0<M.

mod( N, M ) = N, if N < M
mod( N, M ) = mod( N-M, M ), if N >= M

Which of the following methods correctly implements it?

A.   

int mod( int N, int M )
{
  if ( N > M )
    return M;
  else
    return mod( M-N, M );
}

B.   

int mod( int N, int M )
{
  if ( M < N )
    return 1;
  else
    return mod( N-M, M );
}

C.   

int mod( int N, int M )
{
  if ( N < M )
    return N;
  else
    return mod( N-M, M );
}

D.   

int mod( int N, int M )
{
  if ( N < M )
    return N;
  if ( N > M )
    return mod( N-M, M );
}


4. Here is another crazy definition involving integers N and M. Assume 0<=N and 0<M.

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

What is div( 7, 5 ) ?

A.    0

B.    1

C.    2

D.    5


5. What is div( 17, 5 ) ?

A.    1

B.    2

C.    3

D.    4


6. Here is our definition, again, for integers N and M. Assume 0<=N and 0<M.

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

Which one of the following correctly implements it ?

A.   

int div( int N, int M )
{
  if ( N < M )
    return N;
  else
    return div( N-M, M );
}

B.   

int div( int N, int M )
{
  if ( N < M )
    return 0;
  else
    return 1+div( N-M, M );
}

C.   

int div( int N, int M )
{
  if ( N == M )
    return N;
  else
    return div( N-M, M );
}

D.   

int div( int N, int M )
{
  if ( N < M )
    return 0;
  else
    return 1+div( N-M, N );
}


7. Which of the following correctly tests if str is the empty string? (Assume that all the usual methods are available.)

A.    str == ""

B.    str == 0

C.    str.length = 0

D.    str.length() == 0


8. Which of the following evaluates to the first character of str?

A.    str[0]

B.    str[1]

C.    str.substring(0)

D.    str.charAt(0)


9. Here is another definition. Assume that x is a variable that represents a single character and that X represents a string. The operator "+" means concatenation.

rev( "" ) = ""
rev( x+X ) = rev(X) + x

What is rev( "fooey" )?

A.    "ooeyf"

B.    "ooey"

C.    "Fooey"

D.    "yeoof"


10. Here is the definition.

rev( "" ) = ""
rev( x+X ) = rev(X) + x

Which of the following correctly implements it?

A.   

String  rev( String str )
{
  if ( str == ""  )
    return str;
  else
    return rev( str.substring(1) ) + str.charAt(0);
}

B.   

String  rev( String str )
{
  if ( str.length() == 0 )
    return str;
  else
    return rev( str.substring(1) ) + str.charAt(0);
}

C.   

String  rev( String str )
{
  if ( str.length() == 0 )
    return str;
  else
    return rev( str.substring(0) ) + str.charAt(0);
}

D.   

String  rev( String str )
{
  if ( str.length() == 0 )
    return str;
  else
    return rev( str.substring(0) ) + str.charAt(1);
}


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.