CS 2401 Quiz #4

Date: Thursday, July 5, 2012.

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

1a. Can we use recursion for linear or binary searches?

1b. Can we use recursion to determine how many times an element is repeated in an array?

2. Consider the following method:


public class Rotator{
  public static boolean testset(String str){
      return testset(str, 0, str.length()-1);
  }
   
  public static boolean testset(String str, int ii, int jj){ 
    if (jj <= ii)
      return true; 
    else if (str.charAt(jj) != str.charAt(ii))
      return false;
    else  
      return testset(str, ii+1, jj-1);
  }
}
  

2. Trace the execution of the following code to be included in the main method of the Rotator class:

  if (testset("deified"))
     System.out.println("deified is!");
  else
     System.out.println("deified isn't!");
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.