211 Final Review Questions

  1. In java a class and an object are two different things. True/False:
  2. Every constuctor must have a void in place of return type because a constructoor cannot return a value. True/False:
  3. An entire array can be passed as parameter into a method.. True/False:
  4. 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;
      }
    }
    
    output: 
    count is 100 times is 0
  5. 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?
    s1 == s2;          // (a)
    s2 == s3;          // (b)
    s1.equals(s2);     // (c)
    s2.equals(s3);     // (d)
    s1.compareTo(s2);  // (e)
    s2.compareTo(s3);  // (f)
    
    output:
    a: true
    b: true
    c: true
    d: true
    e: 0
    f: 0
  6. 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()
      {
        return serial_no;
      }
    
      public static int getObjectCount()
      {
        return object_count;
      }
      ...
    }
    
    Answer:
    non-static variable serial_no cannot be referenced from a static context return serial_no; ^ cannot resolve symbol variable object_count return object_count; ^ 2 errors
  7. 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.
    Answer:
    
    
      public static int FirstKeyIndex(int a[], int key)
      { 
        for(int i=0; i<a.length; i++)
          if(a[i]==key)
             return i;
        return -1;  
      }
    
      public static int LastKeyIndex(int a[], int key)
      { 
        int value =-1;
        for(int i=0; i<a.length; i++)
      // you may (int i=a.length-1; i>=0; i--) in the above version
          if(a[i]==key)
             value = i;
        return value;  
      }
    
    
    
  8. 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.

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

  10. Using the class ArrayFns from the previous question, complete the following Java application.
    import ArrayFns;
    
    public class ATest
    {
      public static void main(String[] args)
      {
        int[] myArray = { 6, 20, 41, -3, 47, 0, -3 };
        int min;
    
        // TO-DO (1): Assign min to the minimum value.
        // TO-DO (2): Print min to the terminal.
    
        // TO-DO (3): Reverses A.
        // TO-DO (4): Print all elements in A to the terminal.
      }
    }
    

  11. 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; }
    }
    
  12. 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:

  13. 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.