Docsity

Spring 2006 Midterm 2 Answers for CS132, Exams of Computer Science

Toggle sidebar
bg1
CMSC 132 Spring 2006 Midterm 2 Answers
Problem 1 Hashing/Other (12 pts)
1. (4 pts) In Java, what properties must a hashCode() function have? What can go wrong if it
does not have this property?
Answer: Two objects that are equal must have equal hashCodes. Otherwise, equal objects
may not be able to find each other in a HashMap.
2. (4 pts) What other properties are desirable for a hashCode() function? What can go wrong if it
does not have this property?
Answer: Distinct objects should, in general, have distinct and well distributed hashCodes.
Otherwise, there will be a lot of collisions in the HashMap and performance will be poor.
3. (4 pts) What is the expected and worse case asymptotic running time of searching a hash table
of n elements to see if it contains an element?
Answer: Expected O(1), worse case O(n)
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Spring 2006 Midterm 2 Answers for CS132 and more Exams Computer Science in PDF only on Docsity!

CMSC 132 Spring 2006 Midterm 2 Answers Problem 1 Hashing/Other (12 pts)

  1. (4 pts) In Java, what properties must a hashCode() function have? What can go wrong if it does not have this property? Answer : Two objects that are equal must have equal hashCodes. Otherwise, equal objects may not be able to find each other in a HashMap.
  2. (4 pts) What other properties are desirable for a hashCode() function? What can go wrong if it does not have this property? Answer : Distinct objects should, in general, have distinct and well distributed hashCodes. Otherwise, there will be a lot of collisions in the HashMap and performance will be poor.
  3. (4 pts) What is the expected and worse case asymptotic running time of searching a hash table of n elements to see if it contains an element? Answer : Expected O(1), worse case O(n)

Problem 2 Networking (12 pts)

1. (3 pts) Does HTTP use UDP or TCP? Answer : TCP 2. (3 pts) Say you have two computers on the network (machines A and B). Machine A needs to send a short, 4-byte message to machine B, and doesn’t expect to see any message in response. Discuss the merits of using TCP vs. UDP for this communication. Answer : UDP will give fast performance and less overhead, but TCP will give A confirmation that B received the message. 3. (3 pts) Give the first line of a request that a web browser might send to the CNN web server (using the HTTP protocol) to display the page http://www.cnn.com/index.html Answer: GET /index.html HTTP/1. 4. (3 pts) What is the task performed by the accept method of the Java ServerSocket class? Answer: wait until a client connects, and then create a Socket for two-way TCP communication with that client.

Problem 4 Algorithmic Complexity (12 pts)

  1. (3 pts) What is the best asymptotic worst case time complexity for searching for an element in an array of n elements? What if the algorithm only needs to handle sorted arrays? Answer : O(n) and O( log n)
  2. (3 pts) Sort the following complexity categories from least to most complex. O(log( n )) O( n^2 ) O( n log( n )) Answer : O(log( n )) O( n log( n )) O( n^2 )
  3. (6 pts) Calculate the asymptotic complexity of the code snippets below (using big-O notation) with respect to the problem size n: a. f(n) = O( n ) int i=n-4; while (i++ < n) { int k = 0; while (k++ < n) System.out.println("Bye"); } b. f(n) = O( 1 ) for (int k=0; k<n; k += n/2) for (int m=1; m<=n && m <= 30; m++) System.out.println("Hello"); c. f(n) = O( n^2 ) for (int k=0; k<n; k++) for (int m=1; m<=n; m++) System.out.println("Hello"); for(int k = 0; k < 10*n; k++) System.out.println(“Bye”);

Problem 5 Regular Expressions (10 pts)

  1. (3 pts) Write a Java regular expression for the language that includes strings that result from concatenating a single lowercase letter, a single digit and letter A. Examples of strings in the language are: b5A m6A e2A Answer : [a-z][0-9]A
  2. (7 pts) What is the language defined by the following Java regular expression? Describe it in plain English, and then provide 4 test cases of strings in the language, and 4 test cases of strings of a’s, b’s, and c’s that are not in the language. (a|b)+c+ Answer : one or more a’s or b’s, followed by one or more c’s Note that the prefix is allowed to contained both a’s and b’s. Strings in the language: abc, acc, bbbc, babababccc Strings not in the language: aaa, ababa, abaca, emptyString

Problem 7 Linear Data Structures (12 pts)

  1. The following Java class definition is for a singly linked list: public class LinkList { private Node head; // Represents the first node of the list // ADDITIONAL INSTANCE VARIABLES YOU MAY NEED GO HERE class Node { E data; Node next; } } Define a method named add which appends an element at the end of the list. Feel free to add any instance variables you may need. If the constructor needs to initialize any information, provide a constructor. Give the asymptotic efficiency of your add operation as a function of the length of the list (your add function doesn’t have to be as efficient as possible). Answer : public boolean add(E e) { Node newNode = new Node(); newNode.data = e; if (head == null) { head = newNode; return true; } Node tail = head; while (tail.next != null) tail = tail.next; tail.next = newNode; return true; }

Problem 8 Maps/Sets (12 pts) Consider the following class. The main method is primarily provided to ensure that you understand how mapWords will be called; you can probably understand and answer the question without looking at the main method, but it is provided as a backup/auxiliary explanation. public class MapWords { public static void main(String[] args) throws IOException { Map<String, Set> map = new HashMap<String, Set> (); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); int line = 1; String s; while((s = in.readLine()) != null) mapWords(map, s.split(" "), line++); System.out.println(map); }

/** update map by adding lineNumber to Set corresponding to each

String in the array strings; note that if a string has never been seen before, it

will be mapped to null rather than an empty set */

static void mapWords(Map<String, Set> map,

String[] strings, int lineNumber) {

} For example, when run on the input: a b c d e f a d f The class might generate the output {d=[2, 3], a=[1, 3], c=[1], f=[2, 3], b=[1], e=[2]}

  1. (3 pts) Suppose we use a TreeMap rather than a HashMap above. What is the difference in output we will see when we try to print the map? Answer : Keys are printed out in alphabetical order. For example: { a=[1, 3], b=[1], c=[1], d=[2, 3], e=[2], f=[2, 3]}

Honor Section (10 pts)

  1. (5 points) You are studying a problem X, and I don’t think you can develop an efficient algorithm for it. You think problem X is related to graph coloring, which is an NP-complete problem. Sketch, very briefly, how you might be able to find a way to use the relationship between X and graph coloring to show that it is unlikely that you can develop an efficient algorithm for X. Answer : You need to show that if you had a polynomial time algorithm for X, then you could use that algorithm to implement a polynomial time algorithm for graph coloring.
  2. (5 points) You should be familiar with the Fibonacci numbers 1, 1, 2, 3, 5, 8, 13, ...., where fib(1) = fib(2) = 1 and fib(n) = fib(n-1) + fib(n-2). Define a class FibonacciIterator that implements Iterator. Using this iterator will iterate over the Fibonacci numbers. The Iterator should not stop at any point, but just continue generating Fibonacci numbers (you can ignore the fact that eventually you will get overflow with 32 bit integers). Answer : public class FibonacciIterator implements Iterator{ int a = 0; int b = 1; public boolean hasNext() { return true; } public Integer next() { int c = a+b; int result = b; a = b; b = c; return result; } public void remove() { throw new UnsupportedOperationException(); } }