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

  1. What are the advantages of Java's exception handling technique?
  2. Briefly explain the try-throw-catch exception mechanism.
  3. What is the purpose of a finally block? What kind of operations would typically be performed there?
  4. If a try block has multiple catch blocks, the order in which the catch blocks are defined is important. Why is this so?
  5. What is the “Catch or Declare Rule?” How is this rule enforced?
  6. What is the difference between “checked” and “unchecked” exceptions?
  7. What guidelines should be followed when defining your own exception classes?
  8. 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 the CDNotFoundException exception class.
  9. Add the appropriate code to the CDNotFoundException so that getMessage() can return a message that includes the the CD's ID (an integer) in your collection.
  10. Write an exception-controlled loop that loops until the user enters an integer between 1 and 5.
  11. Name two common RuntimeExceptions.
  12. What is a “throws clause” and when is it needed?
  13. 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
   
   //.....
}
  1. What attribute(s) of a method make it a candidate to be a generic method of the Utility class above?
  2. Write the method header for a generic method named First that returns the the first object from an ArrayList<T> of any base type as it would appear in the Utility class above.
  3. Write a small code snippet (variable declarations and method call) that shows how First would be called from main( ) for an ArrayList of objects of type Bob.
  4. Why is it appropriate to use generic programming techniques to implement container classes?
  5. Given the following code syntactically correct code (assume Monarch and Lion are derived classes of Animal):
    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 assigning animaleCage = lionCage; and animalCage = butterflyCage; cause compile errors?
  6. 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.
  7. 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.
  8. 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();
  9. Why is it sometimes necessary to place a bounds on the type parameter of a generic class or generic method?
  10. Explain why a generic class or method may require its type parameter to implement a particular interface.
  11. Explain the meaning of <T extends Comparable<T>> in the method header below.
    public class RClass<T extends Comparable<T>>
  12. 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 these Box operations?
    1. Create a new Box. The capacity is specified by the user when the box is created.
    2. Put an item into the Box
    3. Remove an item from the Box
    4. Tell how many items are in the Box
    5. Empties the Box
    6. Write a declaration for a Box that holds 10 Integers.
    7. Write a declaration for Box that holds 25 XYZ objects.

Containers

  1. Define “container class” and give two examples from the Java library.
  2. 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 );
    
  3. Rewrite your loop to print the ArrayList using an “enhanced for-loop” (a “for-each” loop).
  4. Why in the above code did the implementer choose to use a List reference rather than an ArrayList reference?
  5. Indicate which of the following define an interface and which are concrete classes. For each concrete class, indicate which interface(s) it implements.
    1. Collection<T>
    2. LinkedList<T>
    3. Set<T>
    4. HashSet<T>
    5. TreeSet<T>
    6. ArrayList<T>
    7. List<T>
  6. Conceptually, what does a Collection<T> represent?
  7. What is the primary difference between Set<T> and List<T>?
  8. What is the primary difference between ArrayList<T> and LinkedList<T>?
  9. Name one advantage of an ArrayList over a LinkedList.
  10. Name one advantage of a LinkedList over an ArrayList.
  11. Briefly explain what a Map container is.
  12. Give an example of when one might find a Map a useful container to use.
  13. What is the Collections class used for?

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) {
            // implementation
        }
        public int max(int[] items) {
            // implementation
        }
        public double mean(int[] items) {
            // implementation
        }
        public double median(int[] items) {
            // implementation
        }
    }