Final Exam Study Guide
Picture ID is REQUIRED for all exams
General Topics
- Polymorphism
- Interfaces
- Exceptions
- Generics
- Containers
Use the list of questions below as a guide when studying for the final exam. 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.
Note that the final exam is a comprehensive exam. Although
the bulk of the questions will come from the topics covered since Exam 2,
you should be prepared to answer questions about the important principles
and techniques of OOP covered throughout the semester (e.g. encapsulation,
inheritance, polymorphism, interfaces).
Polymorphism
- Explain the difference between static (early) and dynamic (late) binding.
- Define abstract class. Give an example.
- What is meant by the statement: “Polymorphism refers to the ability to associate many meanings to one method”
- 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.
- What is the clone method, and why is it necessary (in the context of polymorphism)?
- What is the purpose (why would you do it) of making a class abstract?
- When must a class be declared as abstract?
- True/False — Polymorphism refers to how characteristics and behavior can be shared between similar kinds of objects.
- True/False — An abstract class must contain at least one abstract method.
- True/False — An abstract class might not contain any instance variables.
- True/False — An abstract class is considered a data type, just as a concrete class is. Examine this code, then answer the questions below.
- What feature(s) of this code tell you that dynamic binding is taking place?
- What is the output from main() ?
- True/False — The Vehicle class is an abstract class.
- True/False — Motorcycle is derived from Vehicle.
- True/False — Car is derived from Vehicle.
- True/False — setWheels() cannot be used polymorphically.
- When a Car object is created, which constructors are invoked, and in what order?
- What is the purpose of the instance variable
nrVehicles
in theLot
cla ss? - Identify the common coding mistake in
Lot
'spark
method. How would yo u correct this coding error? - In the
Vehicle
constructor, what is the purpose of{ this( 4 ); }
? - In the
MotorCycle
constructor, what is the purpose of{ super( nrWheels); }
? - Car's toString() method invokes getWheels(), even though getWheels( ) is not defined in the Car class. How is this possible?
- Write the copy constructor for the Vehicle class.
- Write the copy constructor for the Car class.
- Write the copy constructor for the Lot class.
- Write the clone( ) method for the Car class.
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"; } }
Interfaces
- Define a Java interface.
- Explain what it means to say that an interface is also a type.
- The
Comparable
interface defines the methodcompareTo
. What are thecompareTo
interface semantics? - Write a
compareTo
method that compares onePerson
object to another. Assume that aPerson
consists of a first name, last name, and age. EachPerson
can be sorted alphabetically by last name, then first name in case of ties on the last name. - What is an interface inconsistency?
- What is the difference between an abstract class and an interface?
- When would one choose to use an abstract class rather than an interface and vice versa?
- Give an example of a programmer-implemented class that might implement two or more pre-written or programmer-implemented interfaces.
- True/False — An interface may only contain method headers.
- True/False — An interface may not contain instance variables.
- True/False — Interfaces may be extended.
- True/False — When a class implements an interface, it must implement all of the methods in that interface.
- True/False — A class can implement only one interface.
- True/False — A class can extend another class, or implement an interface, but not both.
- True/False — The
Comparable
interface is often implemented by classes that require sorting. - True/False — Interfaces are Java's way of simulating multiple inheritance (having more than one base class).
- True/False — The compiler enforces interface semantics.
Exceptions
- What are the advantages of Java's exception handling technique?
- Briefly explain the try-throw-catch exception mechanism.
- What is the purpose of a finally block? What kind of operations would typically be performed there?
- If a try block has multiple catch blocks, the order in which the catch blocks are defined is important. Why is this so?
- What is the “Catch or Declare Rule?” How is this rule enforced?
- What is the difference between “checked” and “unchecked” exceptions?
- What guidelines should be followed when defining your own exception classes?
-
Assume that you have written a program to keep track of your music. If a
user of your program requests a CD that you do not have, the program throws
an exception called
CDNotFoundException
. The program also catches this exception, displays an appropriate error message to the user, then continues. Write the code for theCDNotFoundException
exception class. -
Add the appropriate code to the
CDNotFoundException
so thatgetMessage()
can return a message that includes the the CD's ID (an integer) in your collection. - Write an exception-controlled loop that loops until the user enters an integer between 1 and 5.
-
Name two common
RuntimeExceptions
. - What is a “throws clause” and when is it needed?
- What will happen if a checked exception is never caught?
Generics
Use the following class definition when answering the questions below.
public class Utility { // no instance variables //..... }
-
What attribute(s) of a method make it a candidate to be a generic method
of the
Utility
class above? -
Write the method header for a generic method named
First
that returns the the first object from anArrayList<T>
of any base type as it would appear in theUtility
class above. -
Write a small code snippet (variable declarations and method call) that
shows how
First
would be called frommain( )
for anArrayList
of objects of typeBob
. - Why is it appropriate to use generic programming techniques to implement container classes?
-
Given the following syntactically correct code (assume
Monarch
andLion
are derived classes ofAnimal
):Lion king = new Lion(); Monarch monarch = new Monarch(); Cage<Monarch> butterflyCage = new Cage<Monarch>(); Cage<Lion> lionCage = new Cage<Lion>(); lionCage.add(king); butterflyCage.add(monarch); Cage<Animal> animalCage = new Cage<Animal>(); animalCage.add(king); animalCage.add(monarch);
Why will assigninganimalCage = lionCage;
andanimalCage = butterflyCage;
cause compile errors? -
Describe the use of “
T
” when defining generic classes or methods. In particular, what is the meaning of<T>
,<T extends Animal>
, and<T extends Comparable<T>>
. Give an example definition that uses each of these. -
Describe the use of “wildcards” when defining generic classes
or methods. In particular, what is the meaning of
<?>
,<? extends T>
,<? super T>
, and<T extends Comparable<? super T>>
? Give an example definition that uses each of these. -
Why will the compiler flag the following statement as an error when used
inside of a method within a generic class?
T object = new T();
- Why is it sometimes necessary to place a bounds on the type parameter of a generic class or generic method?
- Explain why a generic class or method may require its type parameter to implement a particular interface.
-
Explain the meaning of
<T extends Comparable<T>>
in the method header below.public class RClass<T extends Comparable<T>>
-
Write a generic class definition for a class named
Box
that contains objects of any type and supports the following operations. What design decisions must be considered for theseBox
operations?-
Create a new
Box
. The capacity is specified by the user when the box is created. -
Put an item into the
Box
-
Remove an item from the
Box
-
Tell how many items are in the
Box
-
Empties the
Box
-
Write a declaration for a
Box
that holds 10Integer
s. -
Write a declaration for
Box
that holds 25XYZ
objects.
-
Create a new
Containers
- Define “container class” and give two examples from the Java library.
-
Why in the code below did the implementer choose to use a
List
reference rather than anArrayList
reference?List<Integer> myList = new ArrayList<Integer>( );
-
Conceptually, what does a
Collection<T>
represent? -
What is the primary difference between
Set<T>
andList<T>
? -
What is the primary difference between
ArrayList<T>
andLinkedList<T>
? -
What is the
Collections
class used for?