MIDTERM_2 - UMass CS !EdLab

advertisement
CmpSci 187
Apr. 14, 2011
Midterm 2
NAME __________________________
ID# ____________________________
Do not begin this exam until we tell you to start.
ALL ANSWERS MUST BE WRITTEN ON YOUR ANSWER SHEET
(we will not grade any work on the exam pages)
Write your name on this exam and both answer sheets – you must turn
in everything when you are done.
If you have a question, walk up to the front of the room to ask your
question as quietly as possible.
When you are done, bring your exam up to the front of the class and
hand it in at the front desk.
1
CmpSci 187
Apr. 14, 2011
Midterm 2
2
Midterm 2
CmpSci 187
Apr. 14, 2011
Multiple Choice (50 points)
enter all answers on the answer sheet
Pick the best answer and pick only one answer.
1. Suppose we implement a queue using a LinkedList. We then have a
(a) a LIFO data structure
(b) a FIFO data structure
(c) a data structure that can remove items in constant time
(d) a data structure that can add items in constant time
(e) (b) and (c) are both true
(f) (b), (c) and (d) are all true
2. The best (most efficient) implementation of a priority queue is managed with
(a) a stack
(b) a LinkedList
(c) an ArrayList
(d) a binary search tree
(e) a heap
(f) a hash table
3. The Comparator interface is satisfied by
(a) any class in the Java API
(b) any user-defined class that contains a properly defined compare method
(c) any user-defined class that extends a Comparator
(d) any primitive data type
(e) (a) and (b) are both true
(f) (b) and (c) are both true
4. 6. A Huffman tree is an example of
(a) a binary tree
(b) a binary search tree
(c) a heap
(d) a priority queue
(e) a linked list
(f) a data type that meets the Comparator interface
3
Midterm 2
CmpSci 187
Apr. 14, 2011
5. If a heap contains 25 items, how many terminal nodes are there?
(a) 8
(b) 10
(c) 13
(d) 15
(e) 20
(f) 25
6. Suppose you are constructing a Huffman tree and you are using a priority queue to
manage the active node list. If your alphabet contains n characters, what is the best possible
runtime complexity for your tree construction algorithm?
(a) O(k)
(b) O(log n)
(c) O(n)
(d) O(n log n)
(e) O(n**2)
(f) O(2**n)
7. Suppose you have an optimal binary search tree for a password directory of 4,000 entries.
What is the maximal number of nodes you will have to visit in order to locate a random entry
in the directory?
(a)
(b)
(c)
(d)
(e)
(f)
11
12
13
15
31
63
8. An open addressing hash table should be rehashed whenever
(a) there are too many AVAILABLE markers in the table
(b) the hash function produces a collision
(c) the load factor passes a certain threshold
(d) the table starts to get too full
(e) (a) and (d) are both true
(f) (c) and (d) are both true
4
CmpSci 187
Apr. 14, 2011
Midterm 2
9. In a hash table that uses chaining, the retrieval of an item that involves collisions is
accomplished
(a) in constant time
(b) in linear time
(c) with additional hash tables
(d) with additional hash functions
(e) (a) and (c) are both true
(f) (a) and (d) are both true
10. Look at the findEntry method definition on p. 403 of your text. Inside findEntry there is a
code snippet:
if (e == AVAILABLE) {
if (avail < 0) avail = i;
}
What would happen if we replaced that with the follow snippet:
if (e == AVAILABLE) {
if (avail < 0) avail = i;
break;
}
(a) findEntry would be fine – it doesn’t matter
(b) findEntry would be fine as long as no items were ever removed
(c) findEntry might fail to locate items that are in the table
(d) findEntry might return an incorrect item
(e) (b) and (c) are both true
(f) (b) and (d) are both true
5
CmpSci 187
Apr. 14, 2011
Midterm 2
Short Answer (30 points) remember to write your answers on the answer sheet
11. Suppose you have the following frequency table: A 10; B 3; C 2; D 4
Draw a Huffman tree for this alphabet. Make sure your tree is consistent with the order property
as used in HW5.
12. Suppose 100 print jobs are loaded into the HW4 printer simulation. The maximal page count
is 60, the priority boundaries are set to 10 and 50, and the printer complex contains 10 printers.
Also, recall that one print job is delivered to the printer complex each minute, and each printer
prints at most 10 pages per minute.
(a) What range of printing times would you expect to see to finish all 100 jobs. Write you answer
as an interval measured in minutes.
(b) Could we get the same runtime interval in (a) with a smaller number of printers? If so, what’s
the smallest number of printers that would manage the print jobs within the same time interval?
13. Look at the hash table trace in Example 9.1 on p. 388 in your text. Assume we are working
with an open addressing hash table and we want to execute all the operations listed in the
“Opertaion” column moving through the operations in order from top to bottom. Draw the
resulting hash table after all the operations have been exceuted in the array below, placing array
location 0 at the top of the display. Note that this is an array of size 7. Apply modulus 7 to all the
map values in order to stay within the range of legal array indices. Don’t forget to include AVAIL
markers as needed.
14. Build a heap for the following list of integers, adding successive integers to the heap one at a
time from left to right. Your resulting heap should place the smallest integer at the root of the
tree. {5, 12, 3, 7, 6, 2, 4, 5, 10, 1}
15. Suppose a text file contains n words and you build a frequency dictionary as in HW6.
(a) What big-O notation describes the computational complexity of that dictionary construction
if the initial hash table is large enough so that no rehashing is required?
(b) Suppose the initial hash table is too small and we need to rehash the table some number of
times in order to complete the dictionary construction. What big-O notation describes the
computational complexity of the dictionary construction in this case?
6
CmpSci 187
Apr. 14, 2011
Code Writing (20 points)
Midterm 2
remember to write your code on the answer sheet
16. (10 points) Suppose you have a dictionary for a text file stored in a binary search tree. Nodes
in the tree are organized according to the alphabetical order of the words found in the text file.
Assume this tree is implemented using the Node<E> class in HW5. Write an instance method for
the Node class named allWords that can be used to print out all the dictionary entries in
alphabetical order.
Hint: here is the Node method that we used to traverse Huffman trees in HW5:
public void traverse(){
System.out.println(contents);
if (left != null) left.traverse();
if (right != null) right.traverse();
}
17. (10 points) Suppose a priority queue of int values has been implemented in a Heap class.
The Heap class contains an array attribute named tree that stores the heap, and an int attribute
named size that records the number of items in the heap. The smallest integer in the heap is at
the root of the heap. The following instance method in the Heap class adds the int value n to the
heap.
. void add(int n){
if (size == 0) tree[1] = n; // correction: we also need to execute size++ in this case
else { tree[1 + size++] = n;
trySwap(size);
}
}
Complete the code needed for this method by defining a recursive method named trySwap.
(Note: for the sake of this exercise, don’t worry about the whether or not the array can run out of
space.)
7
Download