Final Exam Study Guide
Picture ID is REQUIRED for all exams
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).
Interfaces
- Define a Java interface.
- Explain what it means to say that an interface is also a type.
- The
Comparableinterface defines the methodcompareTo. What are thecompareTointerface semantics? - Write a
compareTomethod that compares onePersonobject to another. Assume that aPersonconsists of a first name, last name, and age. EachPersoncan 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
Comparableinterface 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 theCDNotFoundExceptionexception class. -
Add the appropriate code to the
CDNotFoundExceptionso 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
Utilityclass above? -
Write the method header for a generic method named
Firstthat returns the the first object from anArrayList<T>of any base type as it would appear in theUtilityclass above. -
Write a small code snippet (variable declarations and method call) that
shows how
Firstwould be called frommain( )for anArrayListof objects of typeBob. - Why is it appropriate to use generic programming techniques to implement container classes?
-
Given the following syntactically correct code (assume
MonarchandLionare 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
Boxthat contains objects of any type and supports the following operations. What design decisions must be considered for theseBoxoperations?-
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
Boxthat holds 10Integers. -
Write a declaration for
Boxthat holds 25XYZobjects.
-
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
Listreference rather than anArrayListreference?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
Collectionsclass used for?