CS 2401 Quiz #3

Date: Tuesday, February 9, 2010.

1. Please answer questions about the reading assignment (just answer Yes or No, no details needed):

1a. Can we use recursion to convert a binary number into a decimal number?

1b. Can we use recursion to check whether a given string is a palindrome?

1c. Can we use recursion to find the smallest element in an array?

2. Consider the following method:

  public static int problem2(int a, int b){
    int result;
    if (b == 0) 
      {result = 0;} 
    else 
      {result = a + problem2(a, b - 1);} 
    return result;
  }

2a. Trace the execution of the following statement:

  System.out.println(problem2(4, 2));
Reminder: a trace should show the successive calls to the method. For each call, the trace should include the values of the parameters and the value returned to the caller.

2b. What will happen if we trace the following call:

  System.out.println(problem2(4, -2));

2c. For extra credit: what exactly function is computed by the method problem2?