V22.0470 FINAL EXAM

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 = "*";

         }

         System.out.print(hi);

         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.

 

 

 

Need/property

 

Feature/technique/method/class

 

 

1

Requires children to implement a method

 

interface

____

 

 

 

 

 

 

2

Reclaims space associated with unused objects

 

encapsulation

____

 

 

 

 

 

3

Specifies a class's responsibility to clients

 

garbage collection

____

 

 

 

 

 

4

Hides details of implementation

 

this.

____

 

 

 

 

 

5

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

 

protocol

____

 

 

 

 

 

6

Provides only a protocol that may be implemented by appropriate classes

 

abstract

____

 

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