Final

advertisement
NAME:
EE 322C Spring 2007 Final
NAME: _____________________
READ THIS FIRST. Please use the back side of each page for scratch paper. I will not give
credit for illegible answers (and I reserve sole judgment for what is “legible”). The exam is out
of 100 points, not all questions are equally hard or take equally long to complete. Good luck!
1. (6 pts) Add Java statements to the following program such that the program prints the
numbers from 1 to 100 (inclusive).
class Foo {
public int v;
public static void main(String[] args) {
Foo[] x;
// Add code here
for (int k = 0; k < 100; k += 1) {
System.out.println(x[k].v);
}
}
}
2. (6 pts) What is output by the following program?
class Foo {
public int x = 1;
public static int y = 1;
public Foo() {
x = x + 1;
y = y + 1;
}
public static void main(String[] args) {
Foo f;
for (int k = 0; k < 10; k += 1) {
f = new Foo();
}
System.out.println(f.x);
prints:________
System.out.println(f.y);
}
}
prints:________
3. (15 pts) Multiple Guess and short answer.
a. Can a constructor be recursive?
b. A garbage collector will
i. Identify all the the objects referenced by null pointers, and overwrite those null objects
with new objects.
ii. Identify objects that my program is no longer using, and overwrite those old objects
with new ones.
iii. Identify objects that my program can no longer access, and overwrite those
unreachable objects with new ones.
c. Constructors are not static functions in Java because
i. The purpose of a constructor is to initialize a single object. Thus, constructors will
always need this in order to perform their basic function.
ii. Constructors have no return value.
iii. Constructors in Java are invoked when calling “new”. Thus, the memory is always on
the heap and can’t be statically allocated.
iv. Who says that constructors can’t be static? Of course they can.
d. The keyword “private” in java is used to help
i. Undisciplined programmers resist the temptation to hand-optimize their code using
their knowledge of the detailed design of other modules in the system.
ii. The compiler to better optimize programs so that they’ll run faster
iii. The virtual machine to optimize memory so that (private) data takes less space.
iv. Sun Microsystems to optimize profits since almost all major software in use today
requires the the basic protection of private and public
e. Which of the following will guarantee “simple uniform hashing”?
i. Use the built-in hashCode() function (i.e, don’t write your own)
ii. Write a hashCode() function that computes the XOR of all of the bits inside the object
iii. Set M to 1 (i.e., use only one chain)
iv. None of the above
4. (10 pts) For each of the following trees, insert the numbers 1-15 into the nodes of the trees
such that the tree is a valid Binary Search Tree. Every node must have a number
a. (4 pts) Fill in the tree.
b. (3 pts) Fill in the tree
c. (3 pts) How many different correct answers are there to the questions in parts (a) and (b)
assuming that there were N nodes in the tree (note N = 15 for this problem, I want you to
generalize for arbitrarily large N)?
i. 0
ii. 1
iii. N
iv. N log N
v. N! (i.e., N factorial)
5. (10 pts) Consider the following classes, and indicate what the main program will print. I’ve
put in at least one error (sorry, I know that makes the question a bit tricky). The error is not
supposed to be a semicolon missing, or anything idiotic like that, but an error related to
inheritance and how inheritance works in Java. Anyway the error (or errors) will prevent the
program from compiling (which obviously prevent it from running). I want you to identify the
error, circle it, and explain the error. Then, I want you to fill in the rest of the program as if the
line(s) containing error(s) had been removed from your program.
class Cake {
public void eat() { System.out.println(“sawdust”); }
}
class ChocCake extends Cake {
public void eat() { System.out.println(“yummy”); }
}
class ChocTort extends ChocCake {
public int layers;
public ChocTort(int v) { layers = v; }
}
class PoundCake extends Cake {
public void eat() {
System.out.println(“you are what you eat”;
}
}
class Question5 {
public static void main(String[] stupid_param) {
Cake w = new Cake();
Cake x = new ChocCake();
Cake y = new ChocTort();
Cake z = new PoundCake();
w.eat();
// prints: ________________
x.eat();
// prints: ________________
y.eat(); // prints: _________________
System.out.print(y.layers); // prints: _________________
z.eat(); // prints: _________________
}
}
6. (8 points) Heaps:
a. If the values in an array are sorted (smallest values at the start), is it also a MIN HEAP?
b. Given any MIN HEAP, is it possible to print the values in sorted order (smallest to
largest) in O(N) time?
7. (10 pts) Although we don’t normally think of integers as being data structures, they can act a
bit like them. Each integer is actually the product of one or more prime numbers. Hence, an
integer (like the number 60) is actually a data structure containing a sequence of primes (in the
case of 60, the primes are 2, 2, 3, and 5 – note that the prime number 2 is stored in 60 twice).
Write a class PrimeBox and an iterator for it. A PrimeBox is a data structure that is created from
an integer, and contains the sequence of primes for that integer. The iterator “points” at each of
the primes inside the prime box.. For full credit, your solution must have O(1) space for both the
PrimeBox object and the iterator object. Here’s an example of how a PrimeBox and its iterator
might be used.
class TestQuestion {
public static void main(String[] args) {
PrimeBox x = new PrimeBox(60);
Iterator p = x.iterator();
while (p.hasNext()) {
System.out.println(p.next());
}
}
}
class PrimeBox { // your code here
8. (10 pts) In Project 1 we implemented a Set data structure. We built our Set using an array in
which we kept the elements sorted at all times. Some people argue that Set data structures
should be built using Red-Black trees. Assume the operations that can be performed on a Set
are insert (i.e., “add”), find (i.e., “contains”), remove, intersection (i.e., retainAll) and union
(i.e., addAll). Complete the following table by filling in the time complexity for each type of
Set data structure. Assuming that there are n elements in the set, what is the time complexity
of each of the following: Note that for the intersection and union functions, the output data
must be exactly the same type of data structure as the input (i.e., if you take the intersection
of two tree sets, you must get a tree set for the results).
(red-black) Tree-based Set
(sorted) Array-based Set
Insert
Find
Remove
Intersection
Union
9. (6 pts) For each of the following functions indicate if the function would normally be written
recursively or iteratively (assuming a reasonable engineer and a red-black tree data structure)
a. Insert (i.e., “add”)
b. Remove
c. Find Smallest
10. (6 pts) For each of the following operations, indicate which would be faster, a LinkedList or
an ArrayList.
a. Heapsort
b. Binary Search
c. Remove Smallest
11. (8 pts) The Array List class has a fast “append” function because most of the time there is
extra “capacity” at the end of the array – i.e., there is room for more elements than are currently
in the data structure. With each of the following approaches (one of which is the approach
actually used in ArrayList) indicate the total time complexity to append N items (PLEASE,
TOTAL TIME for all N, not the time for just one append).
a. Ensure that the capacity is always between 0 and 1
b. Ensure that the capacity is always between 0 and 100 – that is, when the capacity
is increased, always set the capacity to 100, but don’t increase the capacity again
until it drops to zero.
c. Ensure that the capacity is some random number between 0 and N – that is, when
the capacity is increased, set the capacity to a random number between 0 and N,
don’t change the capacity again until it drops to 0.
d. Ensure that the capacity is always between 0 and M (where M is the number of
elements currently in the data structure). As before, the capacity only changes
when it reaches zero, and it is always set to the current value of M.
12. (5 pts) Write a Java class that could be placed into a HashSet but could not be inserted into a
TreeSet
Download