Ryerson University: Department of Computer Science

CPS209 Final Exam
Winter 2005

Last Name:____________________________________
First Name:____________________________________
Student #:_____________________________________
Section #:_____________________________________
Professor #:         Harley      McInerney           (circle one)


Instructions

Question

Out of

Mark

1

10

         

2

10

         

3

10

         

4

10

         

Total

40

         

Question 1 (CPS209 Final Exam W05)


(a) Define an interface ArrayComparable which has a single method getSmaller. The method getSmaller takes as parameter an array other of Objects and returns an array of those Objects in other which are considered smaller than the object this.
___________________________________________________________________
   
___________________________________________________________________
   
___________________________________________________________________
   



(b) Make additions to the class Person below so that Person implements the interface in part (a). The objects returned by the getSmaller method should be an array of Person objects whose height is less than this.height.
public class Person  __________________________________
{
   private double height ;
   private String name ;

   public Person(double height1, String name1)
   {
      height = height1 ; name = name1 ;
   }

   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
}


(c) Write an equals(Object) method for Person which equates two person objects if they have the same name and height.
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
   ___________________________________________________________________
   
(d) Write a subclass of Person called Driver such that a Driver object has a license (a private instance variable of type String). The equals method of driver should equate two drivers if they are the same in terms of Person equality and if they have equal licenses.
public class Driver ___________________________
{
     ___________________________________________

     public Driver(double height1, String name1, 
                  String license1)
     {
          __________________________________________

          __________________________________________

          __________________________________________

          __________________________________________

     }

     public _____________ equals(Object other)
     {
         _________________________________________________

         _________________________________________________

         _________________________________________________

         _________________________________________________

         _________________________________________________

     }
}

Question 2 MCQ (CPS209 Final Exam W05)


Circle the best answer on this page and mark it also on your "bubble" sheet.
  1. Polymorphism in Java is
    1. the ability to overload methods where the same method may have different patterns depending on input.
    2. that a method call x.doThis() executes code depending on the type of the variable x as opposed to the type of the object referred to by x.
    3. many formed.
    4. that the type of a reference variable determines what methods are available.
    5. that the type of the object to which a reference variable points determines the version of a method which will be executed.
    6. none of the above.
  2. Which of the following is not a difference between MouseListener and ActionListener.
    1. The number of methods.
    2. The return type of the methods.
    3. The type of the parameters.
    4. The typical event source in which it is installed.
    5. none of the above.
  3. Which line would make a mouse listener and install it in a JPanel called panel.
    1. panel.addActionListener(new MouseListener()) ;
    2. panel.addMouseListener(new MouseListener()) ;
    3. panel.add(new MouseListener()) ;
    4. panel.install(new MouseListener()) ;
    5. none of the above
  4. Which of the following methods would serve in a listener to be used with a Timer object such that when the timer ticks a message is printed out telling what time it is (i.e., prints a new Date object).
    1. public void actionPerformed(ActionEvent e) { System.out.println(new Date())} ;
    2. public void mouseClicked(MouseEvent e) { System.out.println(new Date())} ;
    3. public void timeTicked(TimerEvent e) { System.out.println(new Date())} ;
    4. public void timeTicked(TimerEvent e) { System.out.println(Date().toString())} ;
    5. none of the above.
  5. Which line would make the above listener and install it in a new Timer object. Assume that there is an instance variable timer of type Timer and that the above method is in a class called TimerListener.
    1. timer = new TimerListener() ;
    2. timer = new Timer(1000, new TimerListener()) ;
    3. timer.addActionListener(new TimerListener()) ;
    4. addTimer(new TimerListener(timer)) ;
    5. none of the above.
  6. The reason we often define listeners as inner classes is
    1. to give the outer class access to all variables and methods in the inner class.
    2. to give the inner class access to all variables and methods in the outer class.
    3. so that the inner class can be "added" to the outer class
    4. so that the inner class can react to the state of the outer class
    5. none of the above.
  7. One difference between an abstract class and an interface in Java is
    1. an abstract class can have undefined methods but an interface cannot.
    2. an interface cannot be instantiated but an abstract class can.
    3. an abstract class can have implemented methods but an interface cannot.
    4. an interface can have instance variables, but an abstract class cannot.
    5. none of the above.
  8. Explain one similarity between an abstract class and an interface in Java.
    1. Neither can have instance variables.
    2. Both can have instance variables.
    3. Neither can have instance methods.
    4. Neither can have implemented instance methods.
    5. none of the above
  9. Which of the following is not a correct method signature for the class Object.
    1. public boolean equals(Object x)
    2. public int compareTo(Object other)
    3. public String toString()
    4. public Object clone()
    5. none of the above


  10. Which of the following is a superclass, subclass pair
    1. sky, cloud
    2. human body, human leg
    3. human, female
    4. telephone, cellphone
    5. none of the above
  11. Which of the following is a property of flow layout
    1. components have a preferred size
    2. components can be placed in locations such as NORTH or SOUTH
    3. it is the default layout for content pane
    4. components spread to fill the available area.
    5. none of the above
  12. When an exception is caught by a catch clause, where is the next code to be executed after the code in the catch clause?
    1. No code, since after the catch clause code executes the program ends.
    2. The code in the try statement following the point at which the exception occurred.
    3. The code right below the catch clause in your program (even if it is another catch clause).
    4. Either a method called from within the catch clause or no code, but never the code following the catch clause.
    5. none of the above
  13. A "checked exception" is
    1. an exception the jvm watches to see if it occurs.
    2. an exception which the compiler watches to see if you do something about it.
    3. an exception which must be thrown.
    4. an exception which must be caught.
    5. none of the above
  14. Which of the following could not be used to implement the java.util.Set interface:
    1. a linked list
    2. an array list
    3. an array
    4. class Link { Object value ; Link next ; }
    5. none of the above
  15. Why might a hash table be used to implement a set?
    1. Because a hash table generally gives fast look up.
    2. Because a hash code encrypts an object, giving better security.
    3. Because a hash code provides direct one-step access to an object.
    4. Because a hash table forces the uniqueness of elements of the set.
    5. none of the above
  16. The most common reason for using threads in Java programming is
    1. to avoid deadlock.
    2. to allow a program to be run on a multiprocessor machine.
    3. to facillitate coding of tasks which conceptually should run in parallel.
    4. to force the jvm to give equal time to tasks which could be run in parallel.
  17. The purpose of the java reserved word synchronize is to designate methods
    1. which should run in parallel.
    2. which give the current thread a lock on the object.
    3. which cause the current thread to wait.
    4. which cause the other threads to join the wait queue while the current thread is running.
    5. none of the above
  18. The method notifyAll belongs to which class:
    1. Object
    2. Thread
    3. System
    4. Runnable
    5. none of the above
  19. The purpose of the wait method is
    1. to cause the thread to suspend its action for a specified number of milliseconds.
    2. to cause the current thread to suspend its action and release its lock on the object.
    3. to allow the current thread to tell other threads to wait until it is done with the object.
    4. to tell the jvm to suspend all activity until there is a notifyAll command.
    5. none of the above
  20. While thread1 has a lock on object object1,
    1. no other thread can run a synchronized method of object1.
    2. no other thread can run any method of object1.
    3. thread1 is in the wait queue of object1.
    4. no other thread can modify an instance variable of object1.
    5. none of the above

Question 3 (CPS209 Final Exam W05)

Write a program to read words from System.in, one word per line. At end of file your program must print out the word which appeared most often. (In case of a tie for maximum, either word will do).

Question 4 (CPS209 Final Exam W05)


(a) (5 marks) Implement a class called MeasurableStack which has only two methods: public void push(Object), public Object pop(). Assume the usual behavior of these methods. If an attempt is made to pop from the empty stack, then throw a NoSuchElementException. You must base your implementation on the following Link class:
class Link
{
    public Link next ;
    public Object value ;
    public Link(Object value1, Link next1)
    {
       next = next1 ; value = value1 ;
    }
}
(b) (5 marks) Add a recursive method public int total() to the above class. This method assumes that all objects on the stack belong to a class which implements the interface Measurable. Assume that the interface Measurable is defined to have a single method public int getMeasure() which returns an integer measure of the object this. The total method returns the sum of all of the elements in the stack (or zero if there are no elements). Your solution must be recursive -- if it has any iterative construct like for or while, you will get 0 marks for this question (b).