Exam 3 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.
Note that Exam 3 is a comprehensive final 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).
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 code 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 assigninganimaleCage = 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.
-
Given the code fragment below, write a loop that prints the
contents of the
ArrayList
.List<Integer> myList = new ArrayList<Integer>( ); myList.add( 42 ); myList.add( 57 ); myList.add( 95 ); myList.add( 6 ); myList.add( 12 ); myList.add( 105 );
-
Rewrite your loop to print the
ArrayList
using an “enhanced for-loop” (a “for-each” loop). -
Why in the above code did the implementer choose to use a
List
reference rather than anArrayList
reference? -
Indicate which of the following define an interface and which are concrete
classes. For each concrete class, indicate which interface(s) it
implements.
Collection<T>
LinkedList<T>
Set<T>
HashSet<T>
TreeSet<T>
ArrayList<T>
List<T>
-
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>
? -
Name one advantage of an
ArrayList
over aLinkedList
. -
Name one advantage of a
LinkedList
over anArrayList
. -
Briefly explain what a
Map
container is. -
Give an example of when one might find a
Map
a useful container to use. -
What is the
Collections
class used for?
Testing
- What is “white box” testing?
- What is a unit test?
- At what level are unit tests conducted in an OOP language?
- What are some properties of good unit tests?
- What are boundary conditions?
- Give some examples of boundary conditions that you should check for when writing unit tests.
- What are error conditions?
- Give some examples of error conditions that you should check for when writing unit tests.
-
Given the following class, what conditions you would want to test each method for?
public class MathUtils { public int min(int a, int b) { // implementation } public int max(int[] items) { // implementation } public double mean(int[] items) { // implementation } public double median(int[] items) { // implementation } }