Written By Haytham Allos, Fall 2002, NYU


QUESTION 1 (10 points):

Consider the class Flower.  Given your knowledge of some common things about flowers, show a class hierarchy in which the class Flower inherits from another class, which, in turn, inherits from yet another class.  Discuss inheritance from class Flower for other closely related subclasses.

QUESTION 2 (10 points):

What is the output of the following Java program? Assume this code compiles and runs.  In other words, what would I see on the console if I run this program?


class Something {

   static final int MAX_INDEX = 5;

   public static void main(String[] args) {

      int lo = 1;

      int hi = 1;

      String mark="";

      for ( int i = 2; i < MAX_INDEX; i++) {

         if ( hi % 2 == 0 ) {

            mark = "*";



         hi = lo + hi;

         lo = hi -lo;




QUESTION 3 (10 points):

What is the relationship between a class and an object? What is an instance of a class? 


QUESTION 4 (15 points):

Define polymorphism as regards both polymorphic variables and polymorphic methods and give a short example. (Hint: it may help to refer to a base class and a subclass in your answer)


QUESTION 5 (15 points):

Encapsulation is one of the principles of Object Oriented Programming. Describe what is meant by Encapsulation and give an example of how Java supports this principle (Hint: private, public)   

QUESTION 6 (15 points):

The first column, exclusive of the numbers, refers to needs and desirable properties that may arise in Java programming or OO design.  The second column lists features, techniques or Java methods, classes that help to deal with problems or provide desirable outcomes.  Match each feature/technique/method/class to a need/property that it helps to address.  Your answers must be provided by filling in the appropriate "need/property" number to the right of the feature/technique/method/class that is designed to address this need.










Requires children to implement a method











Reclaims space associated with unused objects










Specifies a class's responsibility to clients


garbage collection








Hides details of implementation










Gives an object the ability to refer explicitly to its own instance methods and attributes










Provides only a protocol that may be implemented by appropriate classes





QUESTION 7 (25 points):

Consider the balance of an Account object. Each Account object has an associated History object that records all transactions applied to the account. The query operation getBalance returns the balance of the account, that is, its observable state. To improve performance, the Account object might cache the value of the balance for improving the speed of later queries. To do so, the attribute _balanceCache is introduced which can be updated during a getBalance operation.


Create a class diagram for the above scenario.  This class diagram should include class names, attributes, and methods. (Hint:  You should have two classes.)