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

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

The purpose of this quiz is to have you demonstrate that you can write an elementary Java method correctly, using my definition of "elementary".

  1. What is an advantage of a vector over an array?

    A vector has automatic methods for growing and shrinking the vector, so is easier to use in situations where you need to change the size.

    There are other advantages, but that is the main one.

  2. Write a Java method called divisibleBy that takes two integer parameters x and y, and returns true if x is divisible by y, and false otherwise. Assume that both parameters are positive.

    Here are two solutions.

    Solution 1.

        public static boolean divisibleBy(int x, int y)
        {
          if(x % y == 0) return true;
          else return false;
        }
      

    Solution 2.

        public static boolean divisibleBy(int x, int y)
        {
          return x % y == 0;
        }
      

  3. Write a Java method called sum that takes an array of integers and returns the sum of all of the integers in the array. Assume that the logical and physical sizes of the array are the same, so the entire array is occupied.

    Here are two solutions.

    Solution 1.

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

    Solution 2.

    sumhelp(a,k) returns the sum of the first k members of array a. That is, k is the logical size of a.

        private static int sumhelp(int[] a, int k)
        {
          if(k == 0) return 0;
          else return a[k-1] + sumhelp(a, k-1);
        }
    
        public static int sum(int[] a)
        {
          return sumhelp(a, a.length);
        }
      

  4. Write a Java method called hasNegative that takes an array of integers and returns true if the array contains any negative numbers, and false if all of the numbers in the array are nonnegative. Assume that the logical and physical sizes are the same.

    Here are two solutions.

    Solution 1.

         public static boolean hasNegative(int[] a)
         {
           int k;
           for(k = 0; k < a.length; k++) 
           {
             if(a[k] < 0) return true;
           }
           return false;
         }
      

    Solution 2.

         private static boolean hasNegativeHelp(int[] a, int k)
         {
           if(k == 0) return false;
           else if(a[k-1] < 0) return true;
           else return hasNegativeHelp(a, k-1);
         }
    
         public static boolean hasNegative(int[] a)
         {
           return hasNegativeHelp(a, a.length);
         }
      

  5. Write a Java method that takes two square two-dimensional arrays A and B, and sets array B so that, for every i and j, B[i][j] = A[j][i]. (By square, I mean that all the rows are of the same length, and there are the same number of columns as rows.)

        public static void transpose(int[][] A, int[][] B)
        {
          int i,j;
          for(i = 0; i < A.length; i++) 
          {
            for(j = 0; j < A.length; j++) 
            {
              B[i][j] = A[j][i];
            }
          }
        }
      

  6. Write a Java method called reversal that takes an array s of characters as a parameter, and returns a different array that has the same characters as s, but in the reverse order. For example, if s contains characters 'a', 'b' and 'c', in that order, then reversal(s) should return an array that contains 'c', 'b' and 'a, in that order.

        public static char[] reversal(char[] s)
        {
          int k;
          int[] r;
          r = new char[s.length];
          for(k = 0; k < s.length; k++) 
          {
            r[k] = s[s.length - 1 - k];
          }
          return r;
        }