1. Define: collection, list, set, map.

A collection groups together elements and allows them to be retrieved later. A list is 
a collection that remembers the order of elements. A set is an unordered collection of unique 
elements. A map keeps associations between key and value objects. 

 2. Define: linked list, list iterator.

A linked list is a data structure used for collecting a sequence of objects that 
allows efficient addition and removal of elements in the middle of the sequence. An 
iterator encapsulates a position anywhere inside the linked list. 

 3. Why don't we need iterators with arrays? 

We can simply access each array element with an integer index.

 4. Consider the types HashSet and TreeSet. Do they have anything in common? 

They both implement the Set interface. Set implementations arrange the elements so 
that they can locate them quickly. 

 5. Can you add an element to a set at an iterator position? Explain why or why not. 

It makes no sense to add an element at a particular position in a set, because the 
set can order the elements any way it likes. 

 6. Why are set iterators different from list iterators? 

You do not know, nor can you control, in which order the set keeps the elements. 

 7. Consider the types HashMap and TreeMap. What do they have in common?

The HashMap and TreeMap both implement the Map interface. A map allows you to associate 
elements from a key set with elements from a value collection. You use a map when you want 
to look up objects by using a key.

 8. What is the difference between a set and a map? 

A set stores elements. A map stores associations between keys and values. 

 9. Why is the collection of the keys of a map a set and not a list?

The ordering does not matter and you cannot have duplicates. 

10. Why is the collection of the values of a map not a set?

Because it might have duplicates. 

11. What is a Map<String, HashSet<String>>? Give a possible use for such a structure.

It associates strings with sets of strings. One application would be a thesaurus that 
lists synonyms for a given word. For example, the key "improve" might have as its value 
the set ["ameliorate", "better", "enhance", "enrich", "perfect", "refine"] 

12. What is a hash function? What is a good hash function?

A hash function computes an integer value from an object. A good hash function 
minimizes collisions -- identical hash codes for different objects. 

13. Define: stack, queue, priority queue. 

A stack is a collection of elements with LIFO ("last-in, first-out") retrieval. A queue 0.5
is a collection of elements with FIFO ("first-in, first-out") retrieval. A priority queue
collects elements, each of which has a priority (e.g., collection of work requests, some 
of which may be more urgent than others). In a priority queue elements can be added in 
any order, but when an item is retrieved it is the one with the most urgent priority. 

14. Why would you want to declare a variable as Queue<String> q = new LinkedList<>() 
instead of simply declaring it as a linked list? 

This way, we can ensure that only queue operations can be invoked on the q object. 

15. Why wouldn't you want to use an array list for implementing a queue? 

Depending on whether you consider the 0 position the head or tail of the queue, you would 
either add or remove elements at that position. Both are inefficient operations because all 
other elements need to be moved. 

16. Why wouldn't you want to use a stack to manage print jobs? 

Stacks use a LIFO discipline. If you are the first one to submit a print job and lots of 
people add print jobs before the printer has a chance to deal with your job, they get their 
printouts first, and you have to wait until all other jobs are completed. 

17. What is the value of the reverse Polish notation expression 5 2 6 * 3 * + ? 

(5 2 6 *) -> (5 12 3 *) -> (5 36 +) -> 41

18. Define: big-Oh notation.

Computer scientists use the big-Oh notation to describe the growth rate of a function. 

19. Why can't the Arrays.sort method sort an array of Rectangle objects? 

The Rectangle class does not implement the Comparable interface.

20. When designing a program how do you decide what classes you will need in your program? 

To discover classes, look for nouns in the problem description. Concepts from the problem 
domain are good candidates for classes. 

21. Define: inheritance and aggregation (also called composition).

Inheritance (is-a) is a relationship between a more general class (the superclass) and a 
more specialized class (the subclass). Inheritance is sometimes abused. Aggregation (the 
has-a relationship) often can be used with better results. It denotes that objects of one 
class contain references to objects of another class. 

22. Why should coupling be minimized between classes? 

It is a good practice to minimize the coupling (i.e., dependency) between classes. If the 
change to a class is drastic, all the classes that depend on it (the coupled classes) must 
be updated. Furthermore if we want to use a class in another program all the classes on which 
it depends need to be taken as well. Therefore we always want to remove unnecessary coupling 
between classes. 

23. What is UML? What are CRC cards?

To visualize relationships between classes, such as dependence, programmers draw class 
diagrams. In the textbook we use the UML ("Unified Modeling Language") notation for objects 
and classes. UML is a notation for object-oriented analysis and design invented by Grady Booch,
Ivar Jacobson, and James Rumbaugh (see p. 378, section 8.2 and appendix H for details). 

CRC stands for "classes", "responsibilities" and "collaborators". Use an index card for each 
class. As you think about about verbs in the task description that indicate methods, you pick 
the card of the class that you think should be responsible, and write that responsibility on 
the card. For each responsibility, you record which other classes are needed to fulfill it. 
Those classes are the collaborators. 

24. Define: abstract classes and interfaces.

An abstract method has no implementation. A class that has abstract methods is abstract. A class 
can also be abstract because we declare it that way (and we don't want it to be instantiated ever). 
An abstract class has constructors and constructor chaining rules are 
still in place.  

A Java interface is an element of pure design. A Java interface type declares the methods 
that can be applied to a variable of that type. 

25. Why does a timer require a listener object? What kind of listener is it?

The timer needs to call some method whenever the time interval expires. It calls the actionPerformed 
method of the listener object. java.awt.event.ActionListener. 

26. How do you convert Strings to numbers in Java? 

If a string contains the digits of a number, you use Integer.parseInt(...) or 
Double.parseDouble(...) method to obtain the number value. 

27. Define: command line arguments. How/Where do you get them in your program? 

Programs that start from the command line receive the command line arguments in the main 
method's String[] args. 

28. Encrypt DONALD TRUMP using the Caesar cipher. 

DONALD TRUMP
EPOBME!USVNQ
FQPCNF"VTWOR
GRQDPG#WUXPS

29. When do you use the throw statement? 

To signal an exceptional condition use the throw statement to throw an exception 
object. When you throw an exception, processing continue in an exception handler. 

30. What is the catch clause used for? 

When you throw an exception, processing continue in an exception handler. Place 
the statements that can cause an exception inside a try block and the handler inside 
a catch clause. 

31. What are checked exceptions? 

Checked exceptions are due to external circumstances that the programmer cannot prevent. 
The compiler checks that your program handles these exceptions. A checked exception describes 
a problem that can occur, no matter how careful you are (e.g., an IOException can be caused by 
a disk error or a broken network connection). The unchecked exceptions on the other hand are 
your fault. The compiler does not check whether you handle an unchecked exception (such as an 
IndexOutOfBoundsException) since you should in fact check your index values rather than 
install a handler for that exception. 

32. When do you use the throws clause? 

Add a throws clause to a method that can throw a checked exception. 

33. What does it mean: throw early, catch late? 

Throw an exception as soon as a problem is detected. Catch it only when the problem 
can be handled. 

34. How can you accidentally replicate instance variables from the superclass? Show. 

class A           { int x; A(int x) { this.x = x; } } 
class B extends A { int x; B(int x) { this.x = x; } } // bad, now two x instance variables 
                                                      // ^ (the one in B shadows the one in superclass)
class B extends A {        B(int x) { super(x);   } } // good, uses constructor chaining 

35. What is polymorphism?

A subclass reference can be used when a superclass reference is expected. Polymorphism 
("having multiple forms") allows us to manipulate objects that share a set of tasks, even 
though the tasks are executed in different ways. 

36. Define: dynamic method lookup.

When the virtual machine calls an instance method, it locates the method of the implicit parameter's 
class. The reference to the object might be of one of the superclasses (e.g., Horse), but the method 
run will be from the actual class of the object the reference points to (e.g., Unicorn). This is called 
dynamic method lookup. 

37. When (and why) do you use super with parentheses?

You can also use the reserved word super to invoke a constructor of the superclass (as 
part of the constructor chaining rules). Constructor chaining: unless specified otherwise, 
the subclass constructor calls the superclass constructor with no arguments. To call a 
superclass constructor, use the super reserved word in the first statement of the subclass 
constructor. The constructor of a subclass can pass arguments to a superclass constructor, 
using the reserved word super. 

38. Name two static variables of the System class. What types do they have?

System.out and System.in are the most popular.

java.io.PrintStream and java.io.InputStream

39. Name a static constant of the Math class.

Math.PI and Math.E are my favorite. 

40. What is the JUnit philosophy? 

The JUnit philosophy is to run all tests whenever you change your code. 

41. What is regression testing? 

Regression testing involves repeating previously run tests to ensure that known failures of 
prior versions do not appear in new versions of the software. 

42. How do you simulate the picking of a random playing card? Write code. 

Compute (int)(Math.random() * 4) and associate the result with one of the four suits. 

Compute (int)(Math.random() * 13) and associate with Jack, Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Queen, King. 

--