Computer Science 2310
Spring 2005
Solutions to practice questions for quiz 2

This will be a closed book quiz. You may bring one 8.5x11 page of prepared notes, written on both sides.

  1. What is the value of mystery(4)?

    	public static int mystery(int n)
    	{
    	  if(1 == n) return 1;
    	  else return 3*mystery(n-1);
    	}
    

    Answer: 27

    To solve this, compute mystery(1), mystery(2), mystery(3) and mystery(4).

       mystery(1) = 1
       mystery(2) = 3
       mystery(3) = 9
       mystery(4) = 27
    
    Each is three times the previous one, except for mystery(1), which is 1.

  2. Write a recursive public static method anySevens(n) in Java that returns 1 if there are any 7's in the decimal representation of n, and 0 otherwise. For example, anySevens(974) = 1, anySevens(7) = 1 and anySevens(32) = 0. Function anySevens should require its parameter n to be nonnegative. For this problem, do not use any form of loop, and do not alter the value of any variable that already has a meaningful value.

    Hint. You can get the rightmost digit of n by computing n % 10. You can get all but the rightmost digit by computing n/10. For example, if n is 1974 then n % 10 is 4 and n/10 is 197.

    Here it is in rough pseudo-code.

        Does n have any sevens?
          1. If n = 0, then clearly n has no sevens, so answer no.
          2. If the rightmost digit of n is 7, then clearly n does have a 7,
             so answer yes
          3. Otherwise, ask a friend whether the number that you get by
             removing the rightmost digit from n has any sevens.  Give
             the same answer as the friend gives.
    
    Here it is in Java.
        public static int anySevens(int n)
        {
          if(n == 0) return 0;
          else if(n%10 == 7) return 1;
          else return anySevens(n/10);
        }
    

  3. Suppose that array A contains the following.

        i      A[i]
        0       3
        1       7
        2       8
        3       5
    
    Function mystery is shown below. Starting with A holding what is shown above, what is in array A after running mystery(A,4)?
       public static int mystery(int[] B, int n)
       {
         int k;
         for(k = 0; k < n; k++) 
         {
           B[k] = B[k] + k;
         }
       }
    

        i      A[i]
        0       3
        1       8
        2       10
        3       8
    

  4. What are in array A after running the following Java program fragment? Be careful.

        int[] A = new int[4];
        int k;
        for(k = 0; k < 4; k++) A[k] = k+1;
        for(k = 0; k < 3; k++) A[k+1] = A[k] - 1;
    

        i      A[i]
        0       1
        1       0
        2       -1
        3       -2
    

  5. What is the difference between the logical and physical size of an array?

    The logical size is the number of occupied slots in the array. The physical size is the total number of slots, whether occupied or not.

  6. Write a public static Java method that computes the sum of all of the numbers in an array of integers. There should be two parameters, the array A and the logical size n of the array. Only compute the sum up to the logical size. Use a loop for this function. A heading for this function is shown.

      public static int sum(int[] A, int n)
      {
        int result = 0;
        int k;
        for(k = 0; k < n; k++) result = result + A[k];
        return result;
      }
    

  7. Write a definition of sum that has the same meaning as in the preceding question, but use recursion for this definition instead of a loop.

    If there are no things in the array, then the sum is 0. If there is at least one thing, then the sum of the first n of them is the same as the sum of the first n-1 of them plus the n-th one.

      public static int sum(int[] A, int n)
      {
        if(n == 0) return 0;
        else return sum(A, n-1) + A[n];
      }
    

  8. You would like to set variable s to the sum of all of the integers in array Fish, which has 12 members. What statement would you write to use function sum to do the computation?

        s = sum(Fish, 12);
    

  9. Suppose that s and t are two variables of type String. Write a Java expression that yields true just when s and t hold the same string. (For this question, two strings are the same if they have exactly the same characters, in the same order.)

        s.equals(t)