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. Also, these are not the exact questions that will be on the exam. The form of the questions on the exam may also 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 variable
  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.

Enumeration

  1. Explain the concept of enumeration.
  2. Why would one want to create an enumerated data type?
  3. Give an example (no code) of an enumerated data type that one might create.
  4. What advantages are there to creating an enumerated type for card suits over simply using constants as shown below?
    public static final int SUIT_CLUBS = 0;
    public static final int SUIT_DIAMONDS = 1;
    public static final int SUIT_HEARTS = 2;
    public static final int SUIT_SPADES = 3;
  5. What does the values( ) method do relative to enumerated types?
  6. Write a code snippet demonstrating how the switch statement can be used with an enumerated type.

Inheritance and Polymorphism

  1. Explain the differences among public, private, and protected visibility 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 might 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.

Testing

  1. What is “white box” testing?
  2. What is a unit test?
  3. At what level are unit tests conducted in an OOP language?
  4. What are some properties of good unit tests?
  5. What are boundary conditions?
  6. Give some examples of boundary conditions that you should check for when writing unit tests.
  7. What are error conditions?
  8. Give some examples of error conditions that you should check for when writing unit tests.
  9. Given the following class, what conditions you would want to test each method for?
    public class MathUtils {
        public int min(int a, int b) {
            // returns the smaller of the integers a and b
        }
        public int max(int[] items) {
            // returns the maximum integer in an array of integers
        }
        public double mean(int[] items) {
            // returns the mean (average) integer value of an array of integers
        }
        public double median(int[] items) {
            // returns the medium integer value of an array of integers
        }
    }