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.

    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 "uses 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 function overloading.
  8. Explain the difference between static (early) and dynamic(late) binding.
  9. Define abstract base class. Give an example from one of the course projects.
  10. What is meant by the following statement: "Polymorphism refers to the ability to associate many meanings to one method"?
  11. 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".
  12. What is the clone method, and why is it necessary (in the context of polymorphism)?
  13. True/False

  14. A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  15. In Java inheritance is used to support the "uses-a" relationship between objects.
  16. Aggregation (composition) is used to support the "has-a" relationship between objects.
  17. A derived class has direct access to the base class' private data and methods.
  18. Dynamic binding is performed at run-time when the programmer uses a reference to a base class.
  19. Polymorphism refers to how identical code can produce different effects depending on the actual type of the object being processed.
  20. Polymorphism refers to how structure and behavior can be shared between similar kinds of objects.

    Examine this code, then answer the questions below.

    public class Exam2Code
    {
      public static void main ( String[ ] args )
      {
    	Lot parkingLot;
    
    	Car chevy;
    	Car camry;
    	MotorCycle harley( 3 );
    	MotorCycle honda;
    
    	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 maxVehicles = 20;
      public Lot (  )
      {
    	nrVehicles = 0;
    	vehicles = new Vehicle[maxVehicles];
      }
      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";
      }
    }
    
    

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

  44. Exception classes are different from other classes because they can only contain error messages.
  45. Only one try block is allowed in a program.
  46. A try block can have multiple catch blocks.
  47. Exceptions that are not caught by your program result in a run-time error and core dump.
  48. III. Interfaces
  49. Define a Java interface
  50. Explain what it means to say that an interface is also a type
  51. The Comparable interface defines the method compareTo. What are the Comparable interface semantics?
  52. What is the method header of compareTo?
  53. What is an interface inconsistency?
  54. What is the difference between an abstract class and an interface?
  55. Write the declaration for an array of objects that implement the Comparable inteface
  56. True/False

  57. An interface may only contain method headers
  58. An interface may not contain instance variables
  59. An interface may contain (public, static, final) constants
  60. Interfaces may be extended
  61. When a class implements an interface it must implement all of the methods in that interface
  62. A class can only implement one interface
  63. The Comparable interface is often implemented by classes that require sorting.
  64. Interfaces are Java's way of simulating multiple inheritance (having more than one base class)
  65. The compiler enforces the sematics of the Comparble interface
  66. .

    IV. Inner Classes
  67. Define "inner class"
  68. Describe two advantages of using inner classes
  69. Why do we often mark inner classes as private?

    True/False

  70. An inner class has access to the outer class' private members
  71. An outer class has access to the inner class' private members
  72. To use methods defined in an inner class, the outer class must create an instance of the inner class
  73. What is a static inner class?
  74. How do static and non-static inner classes differ in the way they interact with the outer class?
  75. When should an inner class be marked private? public?
  76. What is an anonymous class?
  77. V. Text File I/O
  78. What is a stream?
  79. What is a text file?
  80. What class is typically used to write to a text file?
  81. How is writing to text file similar to displaying output on the user's screen? Why is this similarity possible?
  82. What does it mean to say that output files are buffered?
  83. What does it mean to say that every file used by your program has two names?
  84. What method should be invoked when your program is finished writing to a file? Why?
  85. What class is typically used to read from a file?
  86. How is reading from a text file similar to getting user input from the keyboard? Why is this similarity possible?
  87. 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.
  88. What is the difference between an absolute path name and a relative path name?