211 Final Review Questions with Solutions
Correction on 11/16 (fri)

  1. What is the output?
    public class Test
    {
      public static void main( String[] args )
      {
        Count myCount = new Count();
        int times = 0;
    
        for (int i = 0; i < 100; i++)
          increment(myCount, times);
    
        System.out.println("count is " + myCount.cnt);
        System.out.println("times is " + times);
      }
    
      public static void increment(Count c, int tm)
      {
        c.cnt++;
        tm++;
      }
    }
    
    public class Count
    {
      public int cnt;
    
      public Count(int num)
      {
        cnt = num;
      }
    
      public Count()
      {
        cnt = 0;
      }
    }
    
    ANSWER: The output would be
    count is 100
    times is 0
    
    In main, myCount is passed to the method increment, and it is modified by c.cnt++; Since myCount is a REFERENCE to an object, the modification in increment indeed makes an effect to myCount (in main) through the parameter reference c. On the other hand, times is a primitive data. So the modification in increment (by tm++;) does NOT affect times in main -- pass by value/copy.

  2. Suppose that s1, s2 and s3 are three strings, given as follows:
    String s1 = "CSC 211";
    String s2 = s1;
    String s3 = "CSC 211";
    
    Assume that s1 and s3 have different memory location. What are the results of the following expressions?

    ANSWER:

    s1 == s2;          // (a) true
    s2 == s3;          // (b) false
    s1.equals(s2);     // (c) true
    s2.equals(s3);     // (d) true
    s1.compareTo(s2);  // (e) 0 -- same content
    s2.compareTo(s3);  // (f) 0 -- same content
    
  3. Is/are there anything (or any things) wrong with the following code? If so, explain why.
    public class Car2
    {
      /* data members */
      ...
      private int serial_no;
      private static int object_counter;
    
      /* methods */
      public Car2(String mdl, String clr, double fl)
      {
        ...
        object_counter++;
        serial_no = object_counter;
      }
    
      public static int getSerialNo()  // error 1
      {
        return serial_no;
      }
    
      public static int getObjectCount()
      {
        return object_count;  // error 2
      }
      ...
    }
    

    ANSWER: There is two errors in the code:

  4. Write the following two static methods. Each method tries to find a key (of type int) in an array of int, and returns an index for the key if it exists, or -1 otherwise.
  5. Given
    double[] a = new double[ 50 ];
    
    write a loop that fills the first cell with 0.5 and the second cell with 1.2, and each succeeding cell with the sum of its two predecessors. For example, the third cell would contain 1.7.

    ANSWER:

    a[0] = 0.5;
    a[1] = 1.2;
    
    for (int i = 2; i < a.length; i++)  // index starts from 2
      ar[i] = ar[i-1] + ar[i-2];        // two preceding elements
    

  6. Write a class called ArrayFns. This class contains two static methods (and no data member):

    ANSWER:

    public class ArrayFns
    {
      public static int ArrayMin(int[] ar)  // static method
      {
        int min = ar[0]; // running min, initalized to a big number
    
        for (int i = 1; i < ar.length; i++)
        {
          if (ar[i] < min)
            min = ar[i];   // a value smaller than the running min updates it
        }
    
        return min;
      }
    
      public static void Reverse(int[] ar)  // static method
      {
        for (int i = 0, j = ar.length-1; i < j; i++, j--)  // CORRECTION: used to be j++
        {
          // swap ar[i] and ar[j] using temp
          int temp = ar[i];
          ar[i] = ar[j];  // assignment to ar[i] indeed make changes in ar
          ar[j] = temp;   // assignment to ar[j] indeed make changes in ar
        }
      }
    }
    
  7. Is/are there anything (or any things) wrong with the following code? If so, explain why.
    public interface Property
    {
      public String get();
      public void set(String s) {}
    }
    
    public class C implements Property
    {
      private String s;
    
      public C() { s = ""; }
      public String get() { return s; }
    }
    
    ANSWER: There are 2 errors:

  8. Write a class Triangle that represents a triangle with corners at specified points. The class has 4 data members: 3 Point (in awt) objects and 1 Color object, and 3 methods:

    ANSWER: See the code linked from the applet html page TriangleApplet.html

  9. Using the class Triangle from the previous question, write an applet which responds to mouse clicks. The applet draws a triangle in blue at/after every third click.

    ANSWER: See the code linked from the applet html page TriangleApplet.html