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. Vocabulary List
    Be able to define these words and use them in the proper context.
  1. base class
  2. super class
  3. derived class
  4. subclass
  5. protected
  6. inherited, inheritance
  7. polymorphism
  8. extends
  9. method overriding
  10. exception class
  11. try, throw, catch, finally
  12. copy constructor, clone
  13. "is a type of"
  14. final
  15. Object class
  16. binding, early binding, late binding, dynamic binding
  17. abstract class, abstract method
  18. super( )
  19. II. Inheritance and Polymorphism
  20. Explain the differences among public, private, and protected member access specifiers. Be sure to mention their role in inheritance.
  21. Explain how inheritance promotes code reuse
  22. Explain the difference between the "is a" and "has a" relationships. Give an example of each from the course projects.
  23. Explain how Java implements "is a type of" relationship
  24. Explain how Java implements "has a" relationship
  25. Give a real life example (other than those used in class) for which inheritance would be appropriate
  26. For the following questions, assume that class D extends class B, that D has some of its own instance variables, some of its own instance methods, and overrides some of B's instance methods.
    1. Which of the two classes is the more general class?
    2. Which of the two classes is the more specific class?
    3. Can the toString() method defined in D call the toString( ) method defined in B? If not, why not. If so, how?
  27. Describe the order in which constructors are called when using inheritance.
  28. Explain the difference between method overriding and method overloading.
  29. Why is method overrding an essential part of polymorphism?
  30. Explain the difference between static (early) and dynamic(late) binding.
  31. What is meant by the following statement: "Polymorphism refers to the ability to associate many meanings to one method"?
  32. In the context of polymorphism, what is meant by the following statement: "The reference type defines which methods may be invoked, but the type of the object defines which implementation is executed".
  33. What is the clone method, and why is it necessary (in the context of polymorphism)?
  34. Explain the statement: "Inheritance allows class similarities to be shared while still allowing for class differences"
  35. What impact does the existance of Object have (if any) when implementing classes?
  36. Explain the statement, "Private methods are effectively not inherited".
  37. What does the keyword super represent? Give one example of its use.
  38. True/False

  39. A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  40. In Java inheritance is used to support the "is a type of" relationship between objects.
  41. Aggregation (composition) is used to support the "has-a" relationship between objects.
  42. A derived class has direct access to the base class' private data and methods.
  43. Dynamic binding is performed at run-time when code uses a reference to a base class to invoke a method.
  44. In Java, dynamic binding is used for all methods.
  45. A class labeled as final cannot be used as a base class.
  46. Polymorphism refers to how identical code can produce different effects depending on the actual type of the object being processed.
  47. Polymorphism refers to how structure and behavior can be shared between similar kinds of objects.
  48. When a derived class is instantiated, its base class no-argument constructor is called whether or not an explicit call to super( ) is made.
  49. When implementing a derived class' copy constructor. its base class copy constructor is automatically called where or not an explicit call to super( ) is made.
  50. 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 );
      }
    }
    
    // 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 static final 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";
      }
    }
    
    

  51. What feature(s) of this code tell you that dynamic binding is taking place?
  52. What is the output from main( ) ?
  53. True/False -- The Vehicle class is an abstract class.
  54. True/False -- Motorcylce is derived from Vehicle.
  55. True/False -- Car is derived from Vehicle.
  56. True/False -- setWheels( ) cannot be used polymorphically.
  57. Write the copy constructor for each class.
  58. Write the clone( ) method for each class (assuming the copy constructor exists)
  59. When a Car object is created, which constructors are invoked, and in what order?
  60. What is the purpose of the instance variable nrVehicles in the Lot class?
  61. Identify the common coding mistake in Lot's park method. How would you correct this coding error?
  62. In the Vehicle constructor, what is the purpose of { this( 4 ); } ?
  63. In the MotorCycle constructor, what is the purpose of { super( nrWheels); }?
  64. Car's toString( ) methods invokes getWheels( ), even though getWheels( ) is not defined in the Car class. How is this possible?
  65. III. Exceptions
  66. What are the advantages of Java's exception handling technique?
  67. Briefly explain the throw-try-catch exception mechanism.
  68. What is the purpose of a finally block? What kind of operations would typically be performed there?
  69. If a try block has multiple catch blocks, the order in which the catch blocks are defined is important. Why is this so?
  70. What is the "Catch or Declare Rule"? How is this rule enforced?
  71. What's the difference between "checked" and "unchecked" exceptions?
  72. What guidelines should be followed when defining your own exception classes?
  73. Name two common RuntimeExcepctions
  74. What is a throws clause and when is it needed?
  75. True/False

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