Version 8.31
See Oncourse Modules for exam locations and dates.
There will be a mixture of question types, possibly including multiple choice, true/false, short answer, essay, and coding. Each student will be allowed one letter-size (8.5x11 inch) sheet of notes for reference, which may be filled on both sides. Calculators may be used during the exam, but probably will not be needed.
Notation conventions: [a, b, c] represents an array; a is the element at index 0; the size (length) of the array is 3. (a, b, c) represents a list with a at the front; the size of the list is 3; the list might be either linked or array-based, and if array-based, the array storing its elements may have 3 or more elements.
Main themes: object-oriented design, Java language, linear data structures, algorithms.
* = question recommended for in-class review.
See Appendix A.
Basics of using the Java programming language: Unix shell commands to compile and run programs; Java code to define classes, declare interfaces, declare variables, define and call methods, use arithmetic expressions and control structures (if, while, for), and use exceptions (try/catch, throw, throws).
What is garbage collection? How does it make a programmer’s life easier?
Encapsulation is “division of a program into distinct components which have limited interaction.” Information hiding = enforcement of encapsulation. Why are encapsulation and information hiding considered to be good things? How do classes, methods, and access levels in the Java language support encapsulation and information hiding?
Polymorphism = “the ability of a word or symbol to mean different things in different contexts.” Explain how these features of the Java language support polymorphism: the Object type, array types, interfaces, and overloading.
Distinguish between primitive and reference types. What is the null reference? What are wrapper classes, autoboxing, and (auto)unboxing?
Distinguish between interfaces and classes.
Inheritance is the passing down of traits (fields and methods) from parent (super, base) classes to child (sub, derived) classes. Name the top class in the Java inheritance hierarchy. Distinguish between “is a” and “has a”.
Distinguish between “overriding” and “overloading”. Why do we commonly override the Object class methods equals and toString?
Know how to use the reserved words extends, super, public, protected, private. Distinguish between the four levels of access in Java, including the default “package” access level.
Name and describe the operations of the Stack interface. Trace the state of a stack through a series of push and pop operations. Describe the role of stacks in executing method calls, including recursive methods.
Name and describe the operations of the Queue interface. Trace the state of a queue through a series of enqueue (add) and dequeue (remove) operations. Know the terms “enqueue” and “dequeue”.
Using Exceptions and “generic” (parameterized) types in Java.
70 10 * 3 4 + /. (List the stack operations and show the result.)Describe the array-based implementations of Stack and Queue, the interface List, and the array-based list. Shrinking and stretching of array-based structures. Iterators: motivation; their methods hasNext, next, and remove. The “enhanced” for statement.
Here is an array-based queue:
| front | size | data | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 6 | 4 |
|
||||||||||||||||
Here is an array-based representation of the list (10, 15, 44, 90, 72):
| size | data | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 5 |
|
||||||||||||||||
Varieties of linked list: single or double link, circular. Advantages/disadvantages of linked lists compared to array-based lists; of doubly linked lists, compared to singly linked. List nodes, the LinkedStack, LinkedQueue, and LinkedList classes. “Two-finger” algorithms for inserting and removing items in a singly linked list.
The functional (recursive, immutable) approach to lists (actually, to the chains of nodes inside lists).
Given a functional List class with operations cons(Object head, List tail), head(List chain), tail(List chain), and with the empty list represented by null, complete the following recursive method definitions. Note that the methods are static and take a List argument; so we would write, for example, cons(x, xs) instead of xs.cons(x).
public static int length (List list) {
// returns the number of elements in this list
if (list == null)
...
else
...
}
public static List replace (Object old_item, Object new_item, List list) {
// returns a new list in which the first occurrence of old_item in the
// original list, if any, is replaced by new_item; otherwise, a copy
// of the original list.
if (list == null)
...
else if ...
...
else
...
}
public static List remove (Object item, List list) {
// returns a new list which is the given list with the first occurrence
// of item, if any, removed; otherwise a copy of the list
if (list == null)
...
else if ...
...
else
...
}(Alfy, Barb, Carla, Dave) — (a) as a singly linked list; (b) as a doubly linked list, (c) as a singly linked circular list.Edwin in the list (Alfred, Barb, Carl, Dave, Fred, Giulia) — (a) in position 2; (b) just before Fred.Define an iterative method for the LinkedList class, p. 168 ff., to compute the sum of a list of Integers.
Be able to analyze the running time of a structured algorithm, stated in pseudocode, or a Java method. The algorithm may use any of the following control structures: sequence, selection, and iteration. Structures may be nested (e.g., nested loops or conditionals).
Know the names of common orders of functions (such as linear and quadratic, page 187) and be able to compare them. For example, is Θ(N2) higher or lower order than Θ(log N)?
Simplify running time functions using asymptotic notation Θ.
Distinguish between best, worst, and average case running time. Distinguish amortized from worst and average case.
Why do we need both theoretical analysis and empirical measurement of running time?
Analyze the worst case running time of the pseudocode fragment below:
read N
sum = 0;
if N > 10:
loop for i from 1 to N:
loop for j from 1 to N:
loop for k from 1 to N:
sum = sum + (i * j * k)
end loop
end loop
end loop
else:
loop for i from 1 to N:
sum = sum + i
end loop
sum = 2 * sum
end if
write sumWhat is the order of the function 20 N5 + 100 N3 + 55 N? Use Θ notation in the simplest form.
How do linear search, binary search, and insertion sort work? What are their running times?
The Comparable interface: motivation and use; the compareTo method.
Be able to: name and describe the two required parts of a recursive algorithm; trace the execution of a recursive algorithm; describe sources of inefficiency in recursion; discuss tail recursion and its elimination.
Be able to set up the recurrence relation (not necessarily to solve it!) for the running time of a recursive function (or method). Be able to give an intuitive analysis of running time for simple cases.
How do merge sort and quicksort work? What are their running times?
Avoiding recursion: why, when (and when not), and (for tail recursion), how?
Show the steps of evaluating the method call barber(21); given the recursive method definition:
int barber (int n) {
if (n < 1)
return 0;
else
return 2 + barber(n - 4);Using the definition below and the array A = [5, -4, 7, 6, 12, 19, 7, 8, 9], trace the call maxArray(A, 0, 8).
public static int maxArray(int [] A, int lo, int hi) {
if (hi == lo)
// Array has one item; return it
return A[lo];
else {
// Find max of each half, and return the larger
int mid = (lo + hi) / 2;
int maxLeft = maxArray(A, lo, mid);
int maxRight = maxArray(A, mid + 1, hi);
if (maxLeft > maxRight)
return MaxLeft;
else
return maxRight;
}
}barber and maxArray (from the previous two questions). Analyze the running time for barber, either by solving the recurrence relation or through informal reasoning.*For the array [55, 49, 27, 300, 185, 211, 99, 85, 42, 150], trace the execution of (a) merge sort; (b) quicksort, pivoting on the last element. “Trace” here means show the array segments on entering and returning from each recursive call.