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 "is a" relationship
  5. Explain how Java implements "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 base 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 invoked, but the type of the object defines which implementation is executed".
  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

  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 where or not an explicit call to super( ) is made.
  28. 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 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";
      }
    }
    
    

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

  53. Exception classes are different from other classes because they can only contain error messages.
  54. Only one try block is allowed in a program.
  55. A try block can have multiple catch blocks.
  56. Exceptions that are not caught by your program result in a run-time error and core dump.
  57. III. Interfaces
  58. Define a Java interface.
  59. Explain what it means to say that an interface is also a type.
  60. The Comparable interface defines the method compareTo. What are the Comparable interface semantics?
  61. What is the method header of compareTo?
  62. What is an interface inconsistency?
  63. What is the difference between an abstract class and an interface?
  64. True/False

  65. An interface may only contain method headers.
  66. An interface may not contain instance variables.
  67. An interface may contain (public, static, final) constants.
  68. Interfaces may be extended.
  69. When a class implements an interface it must implement all of the methods in that interface.
  70. A class can only implement one interface.
  71. The Comparable interface is often implemented by classes that require sorting.
  72. Interfaces are Java's way of simulating multiple inheritance (having more than one base class).
  73. The compiler enforces the sematics of the Comparble interface.
  74. IV. Text File I/O
  75. What is a stream?
  76. What is a text file?
  77. What is a binary file?
  78. What class is typically used to write to a text file?
  79. How is writing to text file similar to displaying output on the user's screen? Why is this similarity possible?
  80. What does it mean to say that every file used by your program has two names?
  81. What method should be invoked when your program is finished writing to a file? Why?
  82. What class is typically used to read from a file?
  83. How is reading from a text file similar to getting user input from the keyboard? Why is this similarity possible?
  84. What mechanism is often used to determine the end-of-file (EOF) condition when reading a text file? Write a short code snippet to show how this mechanism is implemented.
  85. What is the difference between an absolute path name and a relative path name?
  86. What is the purpose of the File class?