Exam 2 Review Questions

Picture ID is REQUIRED for all exams

Use the list of questions below as a guide when studying for Exam 2. It is by no means a comprehensive list of questions.

You are responsible for all material presented in lecture. You are also responsible for all associated reading assignments and material covered in lab.

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

DO NOT EXPECT to see these specific questions on your exam.
    I. Inheritance and Polymorphism
  1. Explain the differences among public, private, and protected member access specifiers. Be sure to mention their role in inheritance.
  2. Explain how inheritance promotes code reuse
  3. Explain the difference between the "is a" and "has a" relationships. Give an example of each from the course projects.
  4. Explain how Java implements the "is a" relationship
  5. Explain how Java implements the "has a" relationship
  6. Describe the order in which constructors are called when using inheritance.
  7. Explain the difference between method overriding and method overloading.
  8. Why is method overrding an essential part of polymorphism?
  9. Define binding.
  10. Explain the difference between static (early) and dynamic(late) binding.
  11. Define abstract class. Give an example from one of the course projects.
  12. What is meant by the following statement: "Polymorphism refers to the ability to associate many meanings to one method"?
  13. In the context of polymorphism, what is meant by the following statement: "The reference type defines which methods may be called, but the type of the object defines which implementation is executed? " Provide simple classes and code snippets that illustrate this important concept.
  14. What is the clone method, and why is it necessary (in the context of polymorphism)?
  15. Explain the statement: "Inheritance allows class similarities to be shared while still allowing for class differences"
  16. True/False
    For each statement which is false, explain why.

  17. A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  18. In Java, inheritance is used to support the "is-a" relationship between objects.
  19. Aggregation (composition) is used to support the "has-a" relationship between objects.
  20. A derived class has direct access to the base class' private data and methods.
  21. Dynamic binding is performed at run-time when the programmer uses a reference to a base class to invoke a method.
  22. In Java, dynamic binding is used for all methods.
  23. A class labeled as final cannot be used as a base class.
  24. Polymorphism refers to how identical code can produce different effects depending on the actual type of the object being processed.
  25. Polymorphism refers to how structure and behavior can be shared between similar kinds of objects.
  26. When a derived class is instantiated, its base class no-argument constructor is called whether or not an explicit call to super( ) is made.
  27. When implementing a derived class' copy constructor. its base class copy constructor is automatically called whether or not an explicit call to super( ) is made.
  28. The protected access specifier should be used for instance variables of a base class that are used in a derived class.
  29. Examine this code, then answer the questions below.

    public class Exam2Code
    {
      public static void main ( String[ ] args )
      {
    	Lot parkingLot;
    
    	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
    {
      public Vehicle( )
    	{ this( 4 ); }
      public Vehicle ( int nrWheels )
    	{ setWheels( nrWheels); }
      public String toString (  )
       	{ System.out.println( "Vehicle with " 
    		+ getWheels() + " wheels"); }
      public int getWheels ( )
       	{ return nrWheels; }
      public void  setWheels ( int wheels )
       	{ nrWheels = wheels; } 
            
      private int nrWheels;
    }
    
    // Parking Lot class
    
    public class Lot
    {
      private final static int MAX_VEHICLES = 20;
      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 (v = 0; v < nrVehichles; v++ )
    		nrWheels += vehicles[ v ].getWheels( );
    	return nrWheels;
      }
      public String toString( )
      { 
    	String s = "";
               
    	for ( v = 0; v < nrVehicles; v++ )
    		s += vehicles[ v ].toString( ) + "\n";
                    
    	return s;
      }
     
      private int nrVehicles;
      private Vehicle [] vehicles;
    }
    
    // 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";
      }
    }
    
    

    For each True/False statement that is False, explain why.

  30. What feature(s) of this code tell you that dynamic binding is taking place?
  31. What is the output from main() ?
  32. True/False -- The Vehicle class is an abstract class.
  33. True/False -- Motorcylce is derived from Vehicle.
  34. True/False -- Car is derived from Vehicle.
  35. True/False -- setWheels() cannot be used polymorphically.
  36. Write the copy constructor for each class.
  37. When a Car object is created, which constructors are invoked, and in what order?
  38. What is the purpose of the instance variable nrVehicles in the Lot class?
  39. Identify the common coding mistake in Lot's park method. How would you correct this coding error?
  40. In the Vehicle constructor, what is the purpose of { this( 4 ); } ?
  41. In the MotorCycle constructor, what is the purpose of { super( nrWheels); }?
  42. Car's toString() methods invokes getWheels(), even though getWheels( ) is not defined in the Car class. How is this possible?
  43. Write the copy constructor for the Vehicle class.
  44. Write the copy constructor for the Car class.
  45. Write the clone( ) method for the Car class.
  46. Write the copy constructor for the Lot class.
  47. II. Exceptions
  48. What are the advantages of Java's exception handling technique?
  49. Briefly explain the throw-try-catch exception mechanism.
  50. What is the purpose of a finally block? What kind of operations would typically be performed there?
  51. If a try block has multiple catch blocks, the order in which the catch blocks are defined is important. Why is this so?
  52. What is the "Catch or Declare Rule"? How is this rule enforced?
  53. What's the difference between "checked" and "unchecked" exceptions?
  54. What guidelines should be followed when defining your own exception classes?
  55. Name two common RuntimeExcepctions
  56. What is a throws clause and when is it needed?
  57. True/False
    For each statement that is false, explain why.

  58. Exception classes are different from other classes because they can only contain error messages.
  59. Only one try block is allowed in a program.
  60. A try block can have multiple catch blocks.
  61. Exceptions that are not caught by your program result in a run-time error and core dump.