QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer STUDENT NAME: ID: The exam consists of five questions. There are a total of 10 points. You may use the back of the sheet, if necessary. No electronic devices (calculators) or notes are allowed. 1. (2 points) (a) State the formal definition of f(n) is O(g(n)). (b) Let f(n) = n 2 + 3n + 5. Use the definition of (a) to show that f(n) is O(n 2 ). (one point for each) (a) There exist two constants c and n 0 such that f(n) cg(n) for all n > n 0. We only gave 0.5 points if the quantifiers are ambiguous. For any c,.. vs There exists a c,... mean very different things. (b) f(n) n 2 + 3n 2 + 5n 2 = 9n 2 for n 1, so c = 9, n 0 = 1 works. Many students derived an answer by dividing both sides of n 2 + 3n + 5 cn 2 by n 2 to get 1 + 3 n + 5 n 2 c and observed that c = 9 works using essentially the same idea as above. Some students, however, concluded that c = 1 works. It doesn t. For example, f(1) = 9 which is not less that 1.
2. (2 points) Give the output of the Tower of Hanoi algorithm for the following: move a tower of height n = 3 from position start = 1 to position finish = 2. It is sufficient for you to give a sequence of (start,finish) pairs, indicating the move at each step, where start and finish are in {1, 2, 3. (1,3) (2,3) (3,1) (3,2) If the solution you gave has more than 7 steps but works (it obeys the constraint that you never have largeer on smaller, and you get all the disks from 1 to 2), then you get 1 point. You need to use 7 steps because the question specifically asks for the Tower of Hanoi algorithm (which uses 7 steps for n = 3).
3. (2 points) State a recurrence relation for the running time t(n) of: (a) decimal to binary conversion (of a positive integer n) (b) mergesort (you may assume the number of elements n is a power of 2) You will not get extra points for solving the recurrences. (1 point for each) (a) We ignored floors and ceilings and accepted anything of the form: t(n) = c + t( n 2 ). (b) t(n) = n + 2t( n 2 ) We gave 0.5 points if you wrote t(n) = 1 + 2t( n 2 ) or t(n) = n + t( n 2 ).
4. (2 points) Consider the Java code: public void display( ArrayList<E> list ){ E e; for (int i = 0; i < list.size(); i++){ e = list.get(i); System.out.println( e.tostring() ); (a) Give an expression for the running time t(n) of this method, where n is the number of elements in the list, i.e. the value returned by method size(). You may write 1 for any operation that takes constant time. (b) Would your answer change if list were declared to be type LinkedList<E>? If so, how? If not, why not? (a) get(i) can be done in constant time for an array and e.tostring() is constant time too. We loop through a list of n elements. Thus, It is fine if you write t(n) = cn t(n) O(n) [If you were not given this mark, let me know.] (b) For a linked list, get(i) requires time proportional to the size of the list. Thus, n(n + 1) t(n) = 1 + 2 + 3 + + n = 2 which is Θ(n 2 ). You get the point if you note that you need to search from the beginnning of the list each time and that this makes it Θ(n 2 ).
5. (2 points) (a) What is the output of the code below, when you run class B? Hint: the output will not change if you rename the s variable within classes A or B. (b) Can the variable v in main be declared as type I or A? Briefly explain. public interface I { public String m(); public abstract class A implements I{ private String s = "A: "; public String m(string s_arg){ return s + s_arg; public class B extends A{ private String s = "B: "; public String m() { return s; public static void main(string[] args){ B v = new B(); System.out.println( v.m( " test ")); (a) A: test Why? The m() method from class A must be used here since the method m in main is passed a String argument and no such method signature exists in B. Thus, B must inherit this method from its superclass A. Without the hint, you might have been confused as to whether or not inheritance implies that the m(string..) code from A also belongs to class B. With the hint, you should see that the code does not belong to B. Thus, class A s m method is used, and this refers to the string s from class A. (b) The object referenced by variable v invokes an m method with String argument. This method is found in A so there would be no problem in declaring v to be type A. However, there is only one m method in interface I and this does not have a String argument. Thus, the compiler gives an error.