CSC 224

Winter 2003

Sample midterm questions

  1. Write a Java applet that displays a row of 12 squares, each 20 pixels by 20 pixels, whose colors alternate between green and blue. Recall that the constants Color.green and Color.blue can be used as arguments to setColor to set the color of a square to green or blue.

  2. What is printed by the following method?
    public static void f()
    {
      int f1 = 1;
      int f2 = 1;
      System.out.println(f1);
      System.out.println(f2);
      for (int index = 3; index <= 10; index++) {
        int next = f1 + f2;
        System.out.println(next);
        f1 = f2;
        f2 = next;
      }
    }
    
  3. Write a method called accumulateArray that takes an integer array parameter A and that returns an integer array B where B[i] is the sum of the entries from A[0] to A[i].   For example, if A contains, in order, the values 12, 8, 19, 25 then the array B that is returned contains the values 12, 20, 39, 64 in that order.
  4. Using the box-and-pointer notation, illustrate the state of memory after the execution of the following statements:
    int[] A = {10, 20, 30, 40};
    int[] B = {50, 60, 70, 80};
    int[] C = new int[4];
    for (int index = 0; index < B.length; index++) {
      C[index] = B[index];
    }
    B = A;