Exam 3 Review Questions

Picture ID is REQUIRED for all exams

Use the list of questions below as a guide when studying for Exam 3. It is by no means a comprehensive list of questions.

You are responsible for all material presented in lecture. Answering the applicable "self-test" questions in each chapter of the optional Savitch text (see the online Syllabus) 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, exceptions).

DO NOT EXPECT to see these specific questions on your exam.

For all TRUE/FALSE questions below which are FALSE, explain why.
    I. Interfaces

  1. Define a Java interface.
  2. Explain what it means to say that an interface is also a type.
  3. The Comparable interface defines the method compareTo. What are the compareTo interface semantics?
  4. Write a compareTo method that compares one Person object to another. Assume that a Person consists of a first name, last name, and age.
  5. What is an interface inconsistency?
  6. What is the difference between an abstract class and an interface?
  7. When would one choose to use an abstract class rather than an interface and vice versa?
  8. Give an example of a programmer-implemented class that might implement two or more pre-written or programmer-implemented interfaces.

    True/False

  9. An interface may only contain method headers.
  10. An interface may not contain instance variables.
  11. Interfaces may be extended.
  12. When a class implements an interface it must implement all of the methods in that interface.
  13. A class can only implement one interface.
  14. The Comparable interface is often implemented by classes that require sorting.
  15. Interfaces are Java's way of simulating multiple inheritance (having more than one base class).
  16. The compiler enforces interface semantics.
  17. II. ArrayList<T>

  18. What are the main advantages of an ArrayList<T> over an array?
  19. Name one advantage of an array over an ArrayList<T>
  20. When would it be necessary to use an array rather than an ArrayList<T>?
  21. Inserting an element into the middle of an ArrayList<T> is a relatively slow operation. Explain why this is so.
  22. Write a Java statement to create an ArrayList to hold DoodleBug objects. The initial capacity of the ArrayList should be 25 DoodleBugs.
  23. Describe the operations of the ArrayList<T> methods get(index), add(object), set(index, object), add(index, object)
  24. What potential programming "pitfall" does the ArrayList<T> clone method present?
  25. Write the code to make a copy of an ArrayList<Double>.

    True/False

  26. An ArrayList<T> object has a fixed size.
  27. The ArrayList<T> sizeOf() method returns the current number of elements allocated to the ArrayList<T>.
  28. You can use any primitive type as the base type of an ArrayList<T> class.
  29. ArrayList<T> objects do not have the array square-bracket notation.
  30. Inheritance is the reason why some parameters of the ArrayList<T> class are of type Base_Type and others of type Object.
  31. The Type parameter is always indicated as a T.
  32. The definition of a "parameterized class" is a class with one or more type parameters.
  33. An ArrayList<T> uses an array to store its elements.
  34. III. Generics

    Use the following class definition when answering the questions below.

    public class Utility
    {
       // no instance variables
       
       //.....
    }
    
  35. What attribute(s) of a method make it a candidate to be a generic method of the Utility class above?
  36. 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.
  37. 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.
  38. Why is it appropriate to use generic programming techniques to implement container classes?
  39. Describe the use of "wildcards" when defining generic classes or methods. In particular, what is the meaning of <?>, <? extends T> and <? super T>? Give an example definition that uses each of these.
  40. 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();

  41. Why is it sometimes necessary to place a bounds on the type parameter of a generic class or generic method?
  42. Explain why a generic class or method may require its type parameter to implement a particular interface.
  43. Explain the meaning of <T extends Comparable<T>> in the method header below.

    public class RClass<T extends Comparable<T>>

  44. 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. Given the class definition for Box above,

    7. Write a declaration for a Box that holds 10 Integers.
    8. Write a declaration for Box that holds 25 XYZ objects.

    IV. Containers and Iterators

  45. Define "container class" and give two examples from the Java library.
  46. Define iterator. What is the purpose of an iterator?
  47. Iterators and the "for-each" loop can both be used to access the elements of any container.
    Under what circumstances would you choose iterators instead of the for-each loop?
    Under what circumstances would you choose the for-each loop instead of an iterator?
  48. Given the code fragment below, write a loop using iterators 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 );
        
         // write your loop here
    
    
  49. What is the output from your loop?
  50. How would the for-loop you wrote in the question above be different if the container were a LinkedList instead of an ArrayList?
  51. Rewrite your loop to print the ArrayList using an "enhanced for-loop".
  52. 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>
  53. Conceptually, what does a Collection<T> represent?
  54. What is the primary difference between Set<T> and List<T>?
  55. What is the primary difference between ArrayList<T> and LinkedList<T>?
  56. Name one advantage of an ArrayList over a LinkedList.
  57. Name one advantage of a TreeSet over a HashSet.
  58. What is the Collections class used for?
  59. True/False

  60. Some containers handle duplicate elements; others do not.
  61. You may instantiate multiple iterators for the same container in the same method.
  62. Iterators are created by the container.
  63. The two base types of Java containers are Collections and Maps
  64. Java collections cannot be used to hold primitive types. That's one reason "wrapper classes" were invented for the primitives.