Exam 2 Study Guide

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. The form of the questions on the exam may 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.

Static Methods and Variables

  1. Define static method and static instance variables
  2. True/False — Static instance variables of a class are shared among all objects of that class.
  3. True/False — Static methods of a class can only access static instance variables and other static methods of that class.
  4. True/False — Every method of a class can access the static data members of that class.
  5. True/False — Static data members are declared and initialized outside the class.
  6. True/False — Static data members may not be final.

Inheritance and Polymorphism

  1. Explain the differences among public, private, default,and protected member access modifiers. Be sure to mention their role in inheritance.
  2. Explain how inheritance promotes code reuse.
  3. Explain the difference between the “uses a,” “is a,” and “has a” relationships. Give an example of each.
  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 overriding 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.
  12. What is meant by the statement: “Polymorphism refers to the ability to associate many meanings to one method”
  13. 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.
  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 allowing for class differences”
  16. What is the base class of all classes in the Java API hierarchy? What are some of the methods in this class that it is usually advisable to override and why?
  17. What is the purpose (why would you do it) of making a class abstract?
  18. When must a class be declared as abstract?
  19. True/False — A base class contains all data and methods which are common to all objects in its inheritance hierarchy.
  20. True/False — A derived class has direct access to its base class' private data and methods.
  21. True/False — Dynamic binding is performed at run-time when the programmer uses a reference to a base class to invoke a method.
  22. True/False — In Java, dynamic binding is used for all methods.
  23. True/False — A class labeled as final cannot be used as a base class.
  24. True/False — Polymorphism refers to how characteristics and behavior can be shared between similar kinds of objects.
  25. True/False — When a derived class is instantiated, its base class no-argument constructor is called whether or not an explicit call to super( ) is made.
  26. True/False — 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.
  27. True/False — The protected access modifier should always be used for instance variables of a base class that are used in a derived class.
  28. True/False — A class can extend more than one base class.
  29. True/False — An abstract class must contain at least one abstract method.
  30. True/False — An abstract class may not contain any instance variables.
  31. True/False — An abstract class is considered a data type, just as a concrete class is.
  32. 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 class?
    9. Identify the common coding mistake in Lot's park method. How would you 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. Design by Contract requires what 3 questions be answered when creating a function of an interface?
  10. True/False — An interface may only contain method headers.
  11. True/False — An interface may not contain instance variables.
  12. True/False — Interfaces may be extended.
  13. True/False — When a class implements an interface, it must implement all of the methods in that interface.
  14. True/False — A class can implement only one interface.
  15. True/False — A class can extend another class, or implement an interface, but not both.
  16. True/False — The Comparable interface is often implemented by classes that require sorting.
  17. True/False — Interfaces are Java's way of simulating multiple inheritance (having more than one base class).
  18. True/False — The compiler enforces interface semantics.