Docsity

Multiple Choice Questions of Object-Oriented Program Data Structure | CS 2110, Exams of Computer Science

Toggle sidebar
bg1
CS211 Spring 2005
Final Exam
May 17, 2005
Solutions
Instructions
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 21
2hours. Good luck!
12345678
Score /5 /4 /4 /7 /20 /4 /4 /10
Grader
9 10 11 12 13 14 15 Σ
Score /6 /6 /5 /5 /10 /6 /4 /100
Grader
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Multiple Choice Questions of Object-Oriented Program Data Structure | CS 2110 and more Exams Computer Science in PDF only on Docsity!

CS211 Spring 2005

Final Exam

May 17, 2005

Solutions

Instructions

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

  1. (5 points) Consider a sorted circular doubly-linked list where the head element points to the smallest element in the list.

(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)

  1. (4 points) Suppose we wish to compute xn^ for integer values of n. We can do this recursively as follows: - if n = 0, return 1 - if n = 1, return x - if n > 1 and n is even, recursively compute y = xn/^2 and return y · y - if n > 1 and n is odd, recursively compute y = xn−^1 and return x · y.

Assuming addition and multiplication are O(1) operations, what is the worst-case asymptotic complexity of computing xn^ this way? O(log n)

  1. (7 points) Prove that for odd n ≥ 1,

∑^ 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

  • (−1)n−1+1(n − 1)^2 + (−1)n+1n^2

splitting off the last two terms

=

(n − 2)(n − 1) 2

  • (−1)n−1+1(n − 1)^2 + (−1)n+1n^2 by the induction hypothesis

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)

  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.

  1. (10 points) Write a recursive delete method for singly-linked lists with integer data that deletes the first occurrence of a given integer from the list and returns the resulting list.

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; }

  1. (6 points) List the sequence of nodes visited by preorder, inorder, and postorder traversals of the following tree: s

s s

s s s

s s s s

HH HH H

   @ @@

@ @@

@ @@

A

C

E F

B

D

G H I J

(a) preorder: ABDGEHICFJ (b) inorder: GDBHEIACJF (c) postorder: GDHIEBJFCA

  1. (6 points) Consider the following directed graph.

s

s

s

s

s s

s s

s s

@ @R

@ @R

@ @R

@ @R

@ @R

@ @R

A

B C

D

E F

G

H I

J

'

&

$

%

?

?

?

?

(a) List the nodes in the order they would be visited in a depth-first search of the graph starting at A. When choosing a node to explore next, break ties in favor of the alphabetically least.

ABDEGHJIFC

(b) List the nodes in the order they would be visited in a breadth-first search of the graph starting at A. When choosing a node to explore next, break ties in favor of the alphabetically least.

ABCDEFGHIJ

(c) How many directed simple cycles does this graph have? 2 0 2 1 2 3 2 9 2 x 27 2 infinitely many

  1. (10 points) Given a binary search tree, write an instance method between(...) that takes a lower bound lower and an upper bound upper and returns a HashSet containing all of the data elements x in the tree between lower and upper, in- clusive. To add an element x to the HashSet h, use h.add(x). (Hint. Define a helper method betweenHelper that takes an extra HashSet argument containing all the elements accumulated so far.)

import java.util.HashSet;

class TreeNode { E value; TreeNode left; TreeNode right;

HashSet between(Comparable lower, Comparable upper) { return betweenHelper(lower, upper, new HashSet()); }

HashSet betweenHelper(Comparable lower, Comparable upper, HashSet h) { if (value.compareTo(upper) <= 0 && value.compareTo(lower) >= 0) h.add(value); if (left != null && value.compareTo(lower) >= 0) h = left.betweenHelper(lower, upper, h); if (right != null && value.compareTo(upper) <= 0) h = right.betweenHelper(lower, upper, h); return h; } }

  1. (6 points) Say what is output by the following programs. If the program gives an error, say what that error is.

(a) int x = 5; try { x = 3; throw new NullPointerException(); } catch (Exception e) { System.out.println(x); } finally { System.out.println(x + 1); }

(b) class A { int foo() { return 1; }} class B extends A { int foo() { return 2; }} class C extends B { int bar(A a) { return a.foo(); }} C x = new C(); System.out.println(x.bar(x));

(c) class A { int e = 1; } class B extends A { int e = 2; } class C extends B { int bar(A a) { return a.e; }} C x = new C(); System.out.println(x.bar(x));

(a) Draw the object that appears on the screen when the code on the previous page is executed.

(b) Draw the object as it appears after the “1970-1974” button is pushed.

End of Exam