







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!








Write your name and Cornell netid above. There are 15 questions on 12 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^12 hours. Good luck!
Score /5 /4 /4 /7 /20 /4 /4 / Grader
Score /6 /6 /5 /5 /10 /6 /4 / Grader
(a) What is the asymptoptic complexity of finding the smallest element in the list? O(1)
(b) What is the asymptoptic complexity of finding the largest element in the list? O(1)
(c) What is the asymptoptic complexity of determining whether a given element e appears in the list? O(n)
(d) What is the asymptotic complexity of finding the median element in the list? O(n)
(e) What is the asymptotic complexity of deleting a given element e in the list (not including the cost of finding it)? O(1)
Assuming addition and multiplication are O(1) operations, what is the worst-case asymptotic complexity of computing xn^ this way? O(log n)
∑^ n
i=
(−1)i+1i^2 = 12 − 22 + 3^2 − 42 + 5^2 − · · · + n^2 =
n(n + 1) 2
By induction on n.
Basis n = 1:
i=1 (−1)
i+1i (^2) = (−1)1+1 12 = 1 = 1(1+1)
Induction step n ≥ 3, n odd: Using strong induction, we would like to show that the theorem is true for n, assuming it is true for all odd m < n. In particular, assume it is true for n − 2. Then
∑^ n
i=
(−1)i+1i^2 =
(n− 2 ∑
i=
(−1)i+1i^2
splitting off the last two terms
=
(n − 2)(n − 1) 2
n(n + 1) 2
by elementary algebra.
We conclude by the strong induction principle that
∑n i=1 (−1)
i+1i (^2) = n(n+1)
(20 points) True or false?
(a) T F The AVL invariant states that a tree’s shortest and longest paths differ in length by at most 1. (b) T F The rotate operation on AVL trees preserves inorder numbering. (c) T F Abstract classes can be instantiated like any other class. (d) T F A stack is a FIFO structure and a queue is a LIFO structure. (e) T F If class A implements interface I, class B extends A, and class C extends B, then C implements I. (f) T F If class A implements interface I, class B extends A, class C extends B, and interface J extends interface I, then C implements J. (g) T F 3 n^ is O(2n). (h) T F log(3n) is O(log(2n)). (i) T F log 3 n is O(log 2 n). (j) T F log(n^2 ) is O(log(n^3 )). (k) T F The optimal number of collisions per bucket in a hashtable is 1. (l) T F If f (n) is O(g(n)) and g(n) is O(h(n)), then f (n) is O(h(n)). (m) T F An occurrence of ’this’ in a static method will cause a compile-time error. (n) T F Binary search is as efficient on linked lists as on arrays, provided the list is doubly linked. (o) T F The finally block in a try-catch-finally statement is executed even if no exception is thrown and there is a return statement in the try block. (p) T F A non-abstract subclass of an abstract class C must provide defini- tions for all the abstract methods of C. (q) T F Downcasting is always legal and can be done without an explicit cast. (r) T F Every dag has a unique topological sort. (s) T F In the worst case, search in an unbalanced binary tree is asymptoti- cally the same complexity as search in a balanced binary tree. (t) T F An ActionListener registered with a JMenuItem is executed auto- matically whenever that JMenuItem is selected.
class ListNode { private int value; //data value public ListNode next; //next element of list, or null if last public ListNode(int v) { value = v; } public int value() { return value; } }
//delete first occurrence of i in list s static ListNode delete(int i, ListNode s) { if (s == null) return s; if (s.value() == i) return s.next; s.next = delete(i, s.next); return s; }
s s
s s s
s s s s
HH HH H
@ @@