





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






CS2110 Spring 2015 Final Write your name and Cornell NetID. There are 8 questions on 9 numbered pages. Check now that you have all the pages. Write your answers in the boxes provided. Use the back of the pages for workspace. Ambiguous answers will be considered incorrect. The exam is closed book and closed notes. Do not begin until instructed. You have 2 ½ hours. Use the backs of the pages if you need more room to write. Be sure to make it clear where an answer is continued. Note: We suggest that you read through the whole test, writing answers to questions that need little thinking and that you can answer quickly and correctly. As you do so, get a sense of the other questions, which require more thought and care. This will allow you to budget your time wisely. Good luck! Have a nice summer break! 1 2 3 4 5 6 7 8 Total Score /20 /8 / 12 / 10 / 10 / 14 / 12 / 14 Grader
1. (20 points) True or false questions a T F A public, non-nested class C must be defined in a file called C.java. b T F It is ok for an interface to declare two methods with the signature void foo(int n). c T F Assume B is a subclass of A and C a subclass of B. It is impossible for any call on a method in an object of class C to execute code in a constructor of A. d T F A type is a set of values e T F Consider a variable v declared as C v;. A call v.m(args) is legal as long as every subclass of C declares method m. f T F Mergesort is a worst-case O(n log n) algorithm. g T F Quicksort is a worst-case O(n log n) algorithm. h T F Dijkstra’s shortest path algorithm is a worst-case O(n log n) algorithm (where n is the number of vertices of the graph) i T F The proof technique of mathematical induction cannot be used if one explicitly proves more than one base case. j T F Suppose class B is a subclass of class C. Then ArrayList is a subclass (or subtype) of ArrayList
3. (12 points) Data structures (a) (4 points) Describe two characteristics of a good hash function and explain why they are necessary. (b) (2 points) Is this a good hash function? Why or why not? public static int hash(int n) { return n % 2; } (c) (6 points) Consider a doubly linked list consisting of Node objects. Class Node has the following structure: public class Node { public Node prev; // points to previous node in list public Node next; // points to next node in list } Complete the following (non-recursive) function: /** Return the middle Node in a list from head to tail inclusive. If the list has an even number of nodes, return the first Node of the middle pair. Note: head and tail are not null. */ public static Node middle(Node head, Node tail) { while (_____________________________________________________) { ___________________________________________________ ___________________________________________________ } return _________________________________________ ; }
4. (10 points) Concurrency You have written a multithreaded GUI library. Each object in the GUI monitors its events (such as mouse clicks) on a separate thread. In other words, there is one thread per class instance. Your colleague decides to use your library to create a GUI program. She creates a central object of class GUI (there is only one such object in the program), with the code shown below. Her program has the following logic: there are several Buttons in the interface. When a Button is clicked, it calls the GUI’s function actionPerformed , in the Button’s thread. The GUI then looks up the address of a webpage associated with the Button in a HashMap and downloads the page content (HTML, images, videos, etc.). This download may take several seconds. To ensure that there are no synchronization errors when several Button threads are simultaneously accessing the HashMap, your colleague synchronizes function actionPerformed as shown below. public class GUI { private HashMap<Object, String> lookupTable= new HashMap(); // Constructor etc. goes here ... /** Handle a button click. / public synchronized void actionPerformed(ActionEvent e) { // Look up the button’s web address in the HashMap String address= lookupTable.get(e.getSource()); // Fetch the webpage content (this may take a while...) downloadWebpage(address); } /* Download the webpage at address, if it is not null. Return only when the download is complete. */ private void downloadWebpage(String address) { ... } } (a) (4 points) Is the synchronization correct? (That is, will it fully avoid race conditions?) Answer YES or NO. If it is not correct, show how to correct it. (You can indicate your changes inline in the code listing above, without rewriting the whole thing.) (b) (6 points) Is the synchronization efficient? Why or why not? Explain briefly, but fully. If your answer is NO, then rewrite the function so that it is both correct and efficient. (You can indicate your changes inline in the code listing above, without rewriting the whole thing.)
6. (14 points) Graphs (a) (2 points) If all edge weights are equal, to what algorithm is Dijkstra’s shortest paths algorithm equivalent? (b) (2 points) What property must a directed graph satisfy in order for topological sort to work on it? (c) (2 points) Kruskal’s algorithm for constructing a spanning tree constructs a set of edges E of the spanning tree. Starting with E empty, at each iteration, it adds to E an edge with minimum weight that does not create a cycle among the edges in E. What does Prim’s algorithm do at each iteration? (d) (2 points) Look at the specification of procedure dfs below, in part (e). Explain what is meant by “a node is REACHABLE from u”, as discussed in lecture. (e) (6 points) Write the body of the following algorithm. You may make it recursive or iterative, but recursive is easier. /** Node u is unvisited. Visit all nodes REACHABLE from u. */ public static void dfs(Node u) { }
7. (12 points) Object-oriented programming (a) (3 points) Write down the three steps in evaluating a new-expression new C(args) Peter, Paul, and Mary form a group to write a graphics program that processes shapes. Their code includes the following two classes: /** An instance is a rectangular shape / public class Rectangle { protected int width; protected int height; public Rectangle(int w, int h) { width= w; height= h; } public int getWidth() { return width; } public void setWidth(int w) { width= w; } public int getHeight() { return height; } public void setHeight(int h) { height= h; } } /* An instance is a square shape. */ public class Square extends Rectangle { public Square(int s) { super(s, s); } public void setSize(int s) { width= s; height= s; } } (b) (4 points) What problem(s) do you anticipate with the above design? Your answer should not talk about the structure of the classes and how they should be different. Instead, your answer should talk about possible behaviors —what a user could do that might cause errors, that might destroy what is expected. (c) (5 points) Rewrite the classes, adding any other code (including new classes or interfaces if necessary) to improve the design and prevent the problem(s) of part (a). There is no single answer here. There are several ways to overcome the possible behavioral problems. Choose one.