Loyola College in Maryland

CS 202 - Computer Science II
Spring 2008


Loyola College > Department of Computer Science > Dr. James Glenn > CS 202 > Homework > Quiz #4 Practice

Due: Wednesday, April 23rd at the beginning of class (but not to hand in)

Read: Wu, Ch. 15

Solutions: here

Problem 1: Consider the following method

public static int bar(String s)
{
  if (s.length() < 2)
    return 0;
  else if (s.charAt(0) != s.charAt(1))
    return 1 + bar(s.substring(1));
  else
    return bar(s.substring(1));
}

Problem 2: Write a recursive method that computes ak, where ak is defined as follows: a0 = 2, a1 = 1, and ak = ak-1 + 2 * ak-2 for k > 1.

Problem 3: Consider the following recursive function.

public int w(int k)
{
  if (k == 0)
    return 2;
  else if (k <= 3)
    return 1;
  else if (w(k - 1) + w(k - 2) + w(k - 3) % 3 == 0)
    return 2;
  else
    return 1;
}
Draw the recursion tree for the call w(5). What value does w(5) return? What value does w(7) return?