Computer Science 2310
Spring 2005
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?

  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.

  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.

  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.

  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)
        
      

  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)