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.
You are also responsible for all associated reading assignments and material covered in lab.
Answering the applicable "self-test" questions in each chapter of the text is also a good way to review.
The answers to the self-test questions are found at the end of each chapter.
Note the 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 (i.e. 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
- Define a Java interface.
- Explain what it means to say that an interface is also a type.
- The
Comparable
interface defines the method compareTo
.
What are the Comparable interface semantics?
- What is the method header of
compareTo
?
- What is the difference between an abstract class and an interface?
- When would choose between an abstract class and an interface?
True/False
- An interface may only contain method headers.
- Interfaces may be extended.
- When a class implements an interface it must implement all of
the methods in that interface.
- A class can only implement one interface.
- The
Comparable
interface is often implemented by classes
that require sorting or searching.
- Interfaces are Java's way of simulating multiple inheritance (having more
than one base class).
- The compiler enforces the sematics of the
Comparble
interface.
II. ArrayList
- What are the main disadvantages of an ArrayList?
- What are the main advantages of an ArrayList over an array?
- When would it be necessary to use an array rather than an ArrayList?
- Why might you choose an array instead of an ArrayList?
- Inserting an element into the middle of an ArrayList is a relatively slow operation. Explain why this is so.
- Removing an element from the middle of an ArrayList is a relatively slow operation. Explain why this is so.
- Write a Java statement to create an ArrayList to hold DoodleBug objects. The initial capacity of
the ArrayList should be 25 DoodleBugs.
- Describe the operations of the ArrayList methods
get(index), add(object), set(index, object), add(index, object), remove(index), remove(object)
- What common exception is thrown by many of the ArrayList methods listed above? Under what conditions will this exception be thrown?
- What potential programming "pitfall" does the ArrayList
clone
method present?
True/False
- An ArrayList object has a fixed size.
- You can use any primitive type as the base type of an ArrayList class.
- ArrayList objects do not have the array square-bracket notation.
- The Type parameter is always indicated as a T.
- The definition of a "parameterized class" is a class with one or more type parameters.
- An ArrayList uses an array to store its elements
III. 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 in the Utility class above?
- Write the method header for a generic method named
First
that returns the
the first object from an ArrayList of any base type as it would appear in the Utility class above.
- Assuming the definition
ArrayList<Integer> myInts = new ArrayList<Integer>( );
write the Java statement that invokes First
(from the previous question) using myInts
as a parameter.
- Why is it appropriate to use generic programming techniques to implement container classes?
- Describe the use of "wildcards" when defining generic classes or methods. In particular,
state the meanings of <?>, <? extends T> and <? super T>. Give an example definition that uses each of these.
- Explain why a generic class or method may require its type parameter to implement a particular interface.
- 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?
- Create a new Box. The capacity is specified by the user when the box is created.
- Puts an item into the
Box
- Removes an item from the
Box
- Tells how many items are in the
Box
- Empties the box
Given the class definition for Box
above
- Write a declaration for a
Box
that holds 10 Integers
- Write a declaration for
Box
that holds 25 XYZ objects
- Generic classes and methods sometimes require that objects used in place of their
type parameter implement a particular method (or methods). One way to do this is for the
generic class or method to require the parameter type to implement a particular interface.
Another way is to require the parameter type to belong to a particular class hierarchy.
- Write the class header for a generic class named
Truck<T>
that requires its
type paramter to implement the Comparable<T>
interface.
- Write the class header for a generic class named
Truck<T>
that requires its
type paramter to belong to a class hierachy whose base class is name Fred
.
- Explain (or give an example) when each of these techniques is most appropriate.
IV. Containers and Iterators
- Define "container class" and give two examples from the Java library.
- What Java mechanism is used to define containers?
- Define iterator. What is the purpose of an iterator?
- 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.
- 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( new Integer(42) );
myList.add( new Integer(57) );
myList.add( new Integer(95) );
myList.add( new Integer(6) );
myList.add( new Integer(12) );
myList.add( new Integer(105) );
// write your loop here
- What is the output from your loop?
- How would the for-loop you wrote in the question above be different if the container
were a LinkedList instead of an ArrayList?
- Rewrite your loop to print the ArrayList using an "enhanced for-loop".
- 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>
- What is the primary difference between Set<T> and List<T>?
- What is the primary difference between ArrayList<T> and LinkedList<T>?
True/False
- All containers allow duplicate elements.
- You may instantiate multiple iterators for the same container in the same program/function.
- Iterators are created by the container.
- The two types of Java containers are Collections and Maps
- Java Collection cannot be used to hold primitive types. That's one reason why "wrapper classes" were invented for the primitives.