Final Exam Study Guide

Picture ID is REQUIRED for all exams

General Topics

Use the list of questions below as a guide when studying for the final exam. It is by no means a comprehensive list of questions. Also, these are not the exact questions that will be on the exam. The form of the questions on the exam may also be different from those found here.

You are responsible for all material presented in lecture. You are also responsible for all material covered in lab and the concepts taught by any programming assignments.

Answering the applicable self-test questions in each chapter of the recommended text (by Savitch) is also a good way to review. The answers to the self-test questions are found at the end of each chapter.

Note that the final exam is a comprehensive exam. Although the bulk of the questions will come from the topics covered since Exam 2, you should be prepared to answer questions about the important principles and techniques of OOP covered throughout the semester (e.g. encapsulation, inheritance, polymorphism, interfaces).

Polymorphism

  1. Explain the difference between static (early) and dynamic (late) binding.
  2. Define abstract class. Give an example.
  3. What is meant by the statement: “Polymorphism refers to the ability to associate many meanings to one method”
  4. In the context of polymorphism, what is meant by the statement: “The reference type defines which methods may be called, but the type of the object referenced defines which implementation is executed”? Provide simple classes and code snippets that illustrate this important concept.
  5. What is the clone method, and why is it necessary (in the context of polymorphism)?
  6. What is the purpose (why would you do it) of making a class abstract?
  7. When must a class be declared as abstract?
  8. True/False — Polymorphism refers to how characteristics and behavior can be shared between similar kinds of objects.
  9. True/False — An abstract class must contain at least one abstract method.
  10. True/False — An abstract class might not contain any instance variables.
  11. True/False — An abstract class is considered a data type, just as a concrete class is.
  12. Examine this code, then answer the questions below.

    public class Exam2Code
    {
      public static void main ( String[ ] args )
      {
            Lot parkingLot = new Lot();
    
            Car chevy = new Car( );
            Car camry = new Car( );
            MotorCycle harley = new MotorCycle( 3 );
            MotorCycle honda = new MotorCycle( );
    
            parkingLot.park ( chevy );
            parkingLot.park ( honda );
            parkingLot.park ( harley );
            parkingLot.park ( camry );
    
            System.out.println( parkingLot.toString( ) );
      }
    }
    
    // Vehicle class
    public class Vehicle
    {
      private int nrWheels;
    
      public Vehicle( )
            { this( 4 ); }
      public Vehicle ( int nrWheels )
            { setWheels( nrWheels); }
      public String toString (  )
            { return "Vehicle with " + getWheels()
                     + " wheels"; }
      public int getWheels ( )
            { return nrWheels; }
      public void setWheels ( int wheels )
            { nrWheels = wheels; }
    }
    
    // Parking Lot class
    
    public class Lot
    {
      private final static int MAX_VEHICLES = 20;
      private int nrVehicles;
      private Vehicle [] vehicles;
    
      public Lot (  )
      {
            nrVehicles = 0;
            vehicles = new Vehicle[MAX_VEHICLES];
      }
    
      public int nrParked (  )
            { return nrVehicles; }
      public void park ( Vehicle v )
            { vehicles[ nrVehicles++ ] = v; }
      public int totalWheels ( )
      {
            int nrWheels = 0;
            for (int v = 0; v < nrVehicles; v++ )
                    nrWheels += vehicles[ v ].getWheels( );
            return nrWheels;
      }
      public String toString( )
      {
            String s = "";
    
            for (Vehicle v : vehicles){
                    if(v != null){
                            s += v.toString( ) + "\n";
            }
        }
            return s;
      }
    }
    
    // MotorCycle class
    public class MotorCycle extends Vehicle
    {
       public MotorCycle ( )
            { this( 2 ); }
       public MotorCycle( int wheels )
            { super( wheels ); }
    
    }
    
    // Car class
    public class Car extends Vehicle
    {
      public Car ( )
            { super( 4 ); }
      public String toString( )
      {
            return "Car with " + getWheels( ) + " wheels";
      }
    }
    
    1. What feature(s) of this code tell you that dynamic binding is taking place?
    2. What is the output from main() ?
    3. True/False — The Vehicle class is an abstract class.
    4. True/False — Motorcycle is derived from Vehicle.
    5. True/False — Car is derived from Vehicle.
    6. True/False — setWheels() cannot be used polymorphically.
    7. When a Car object is created, which constructors are invoked, and in what order?
    8. What is the purpose of the instance variable nrVehicles in the Lot cla ss?
    9. Identify the common coding mistake in Lot's park method. How would yo u correct this coding error?
    10. In the Vehicle constructor, what is the purpose of { this( 4 ); } ?
    11. In the MotorCycle constructor, what is the purpose of { super( nrWheels); }?
    12. Car's toString() method invokes getWheels(), even though getWheels( ) is not defined in the Car class. How is this possible?
    13. Write the copy constructor for the Vehicle class.
    14. Write the copy constructor for the Car class.
    15. Write the copy constructor for the Lot class.
    16. Write the clone( ) method for the Car class.

Interfaces

  1. Define a Java interface.
  2. Explain what it means to say that an interface is also a type.
  3. The Comparable interface defines the method compareTo. What are the compareTo interface semantics?
  4. Write a compareTo method that compares one Person object to another. Assume that a Person consists of a first name, last name, and age. Each Person can be sorted alphabetically by last name, then first name in case of ties on the last name.
  5. What is an interface inconsistency?
  6. What is the difference between an abstract class and an interface?
  7. When would one choose to use an abstract class rather than an interface and vice versa?
  8. Give an example of a programmer-implemented class that might implement two or more pre-written or programmer-implemented interfaces.
  9. True/False — An interface may only contain method headers.
  10. True/False — An interface may not contain instance variables.
  11. True/False — Interfaces may be extended.
  12. True/False — When a class implements an interface, it must implement all of the methods in that interface.
  13. True/False — A class can implement only one interface.
  14. True/False — A class can extend another class, or implement an interface, but not both.
  15. True/False — The Comparable interface is often implemented by classes that require sorting.
  16. True/False — Interfaces are Java's way of simulating multiple inheritance (having more than one base class).
  17. True/False — The compiler enforces interface semantics.

Exceptions

  1. What are the advantages of Java's exception handling technique?
  2. Briefly explain the try-throw-catch exception mechanism.
  3. What is the purpose of a finally block? What kind of operations would typically be performed there?
  4. If a try block has multiple catch blocks, the order in which the catch blocks are defined is important. Why is this so?
  5. What is the “Catch or Declare Rule?” How is this rule enforced?
  6. What is the difference between “checked” and “unchecked” exceptions?
  7. What guidelines should be followed when defining your own exception classes?
  8. Assume that you have written a program to keep track of your music. If a user of your program requests a CD that you do not have, the program throws an exception called CDNotFoundException. The program also catches this exception, displays an appropriate error message to the user, then continues. Write the code for the CDNotFoundException exception class.
  9. Add the appropriate code to the CDNotFoundException so that getMessage() can return a message that includes the the CD's ID (an integer) in your collection.
  10. Write an exception-controlled loop that loops until the user enters an integer between 1 and 5.
  11. Name two common RuntimeExceptions.
  12. What is a “throws clause” and when is it needed?
  13. What will happen if a checked exception is never caught?

Generics

Use the following class definition when answering the questions below.

public class Utility
{
   // no instance variables
   
   //.....
}
  1. What attribute(s) of a method make it a candidate to be a generic method of the Utility class above?
  2. Write the method header for a generic method named First that returns the the first object from an ArrayList<T> of any base type as it would appear in the Utility class above.
  3. Write a small code snippet (variable declarations and method call) that shows how First would be called from main( ) for an ArrayList of objects of type Bob.
  4. Why is it appropriate to use generic programming techniques to implement container classes?
  5. Given the following syntactically correct code (assume Monarch and Lion are derived classes of Animal):
    Lion king = new Lion();
    Monarch monarch = new Monarch();
    Cage<Monarch> butterflyCage = new Cage<Monarch>();
    Cage<Lion> lionCage = new Cage<Lion>();
    lionCage.add(king);
    butterflyCage.add(monarch);
    Cage<Animal> animalCage = new Cage<Animal>();
    animalCage.add(king);
    animalCage.add(monarch);
    
    Why will assigning animalCage = lionCage; and animalCage = butterflyCage; cause compile errors?
  6. Describe the use of “T” when defining generic classes or methods. In particular, what is the meaning of <T>, <T extends Animal>, and <T extends Comparable<T>>. Give an example definition that uses each of these.
  7. Describe the use of “wildcards” when defining generic classes or methods. In particular, what is the meaning of <?>, <? extends T>, <? super T>, and <T extends Comparable<? super T>>? Give an example definition that uses each of these.
  8. Why will the compiler flag the following statement as an error when used inside of a method within a generic class?
    T object = new T();
  9. Why is it sometimes necessary to place a bounds on the type parameter of a generic class or generic method?
  10. Explain why a generic class or method may require its type parameter to implement a particular interface.
  11. Explain the meaning of <T extends Comparable<T>> in the method header below.
    public class RClass<T extends Comparable<T>>
  12. Write a generic class definition for a class named Box that contains objects of any type and supports the following operations. What design decisions must be considered for these Box operations?
    1. Create a new Box. The capacity is specified by the user when the box is created.
    2. Put an item into the Box
    3. Remove an item from the Box
    4. Tell how many items are in the Box
    5. Empties the Box
    6. Write a declaration for a Box that holds 10 Integers.
    7. Write a declaration for Box that holds 25 XYZ objects.

Containers

  1. Define “container class” and give two examples from the Java library.
  2. Why in the code below did the implementer choose to use a List reference rather than an ArrayList reference?
        List<Integer> myList = new ArrayList<Integer>( );
        
  3. Conceptually, what does a Collection<T> represent?
  4. What is the primary difference between Set<T> and List<T>?
  5. What is the primary difference between ArrayList<T> and LinkedList<T>?
  6. What is the Collections class used for?