Sie sind auf Seite 1von 8

CS 112 Fall 1998 Midterm Exam #1

Your name: Your student number: Circle instructor: borgida/iacono/morris/smith TAs Name:

Do not open the exam until instructed to do so. After you begin, be sure your test has seven questions over eight pages. The eighth page is blank. Do not sit next to anyone with whom you have studied. Be sure your student number is on this page. Be sure your name is on each page. One page of notes is allowed. Working on the exam after time has been called is considered cheating, and you will receive a 0 on the exam if you fail to turn in the exam when asked to do so. Use your time eciently. You may not be able to nish every problem in the time allowed. If you are taking this exam to make up a grade for a previous semester, please explain your circumstances below. A list of classes and methods from the project is provided at the end of the exam.

Problem Possible 1 30 2 26 3 26 4 16 5 18 6 14 7 20 Total 150

Score

Name: 1. (30 points) On complexity

CS 112 Midterm #1, Fall 1998

Evaluating a polynomial of the form f (y ) = a0 + a1 y 1 + a2 y 2 + . . . + an y n can be done using the following algorithm, if the coecients are stored in an array a, and the value for the argument is in variable y:

double answ=0.0; for (int j=0 ; j<=n ; j++) { double t = 1.0 ; for (int e=1 ; e<=j ; e++) {t = t * y;} answ = asnw + a[j]*t; } //return answ
(a) Which assignment statement or comparison is executed most often?

(b) Using big-oh notation give the worst-case complexity of this algorithm? (Give as tight a bound as possible.)

(c) Use the fact that y n = y y (n1) to improve the previous algorithm so that its complexity order is reduced. Provide the code below.

(d) What is the worst-case order of the improved algorithm?

(e) What is the best-case complexity order of the original algorithm?

Name:

CS 112 Midterm #1, Fall 1998

Questions 2 and 3 below refer to Assignment 1. For your reference, the complete list of methods for it appears at the end of the exam. 2. (26 points) In the class CrossReferenceEntry from Assignment 1, the method add adds a line number to an existing entry. Each use of an identier is recorded resulting in the possibility of the same line number occuring multiple times for one identier. Provide below an add method for the class CrossReferenceEntry that records each line on which an identier is found but does NOT include multiple instances of the same line number. class CrossReferenceEntry { private OrderedList lines ; ... public void add(int ln) { /* Add your code here */

3. (26 points) You want to maintain corporations in an OrderedList from wealthiest to poorest based on their net worth. To do this the class Corp must implement the Comparable interface. Complete the implementation of the interface Comparable for the class Corp given below. Be sure that that when inserted into an OrderedList the result will be ordered from wealthiest to poorest. Grades will be based on correctness, eciency, simplicity, and eective reuse of code. class Corps implements Comparable { private int netWealth ; // contains a corporations value in dollars ... public boolean lessThan(Comparable x) { Corp xc = (Corp) x;

} public int compares(Comparable x) { Corp xc = (Corp) x;

Name:

CS 112 Midterm #1, Fall 1998

4. (16 points) Tracing linked list operations Consider the following program. public class LinkedNode { public Object data; public LinkedList next; } public void dosomething(LinkedNode L) { LinkedNode q = p.next p.next = q.next; LinkedNode r = p.next.next; q.next.next = q; p.next.next.next = r }

1 2 3 4 5

You will be asked to trace the code above, line by line, by completing the diagrams with arrows for links. Suppose that before step 1, the diagram for the linked list is the following: p a b d c / // q

Show all links, along with p and q after line 1 has run in the diagram below:

Show, in the diagram below, all links, along with p and q after line 2 has run int the diagram below:

Show all links, along with p,q, and r after line 3 has run in the diagram below:

Show all links, along with p,q, and r after line 4 has run in the diagram below:

Show all links, along with p,q, and r after line 5 has run in the diagram below:

Name: 5. (18 points) Using data structures.

CS 112 Midterm #1, Fall 1998

The following are interfaces for Stack and Queue, indicating the functions available to you. public class Stack { Stack(); void push(Object); Object pop(); //topAndPop from the text boolean isEmpty(); void makeEmpty(); } public class Queue { Queue(); void enqueue(Object); Object dequeue(); boolean isEmpty(); void makeEmpty(); }

Given some a variable, stk of type Stack, you are required to compute its size. You do not know the internal representation of the stack; size is not a method of Stack; and at the end, stk must behave no dierently than it did at the beginning (i.e., same values, in the same order). If you wish, you may use an auxiliary data structure, in a variable called helper of type Stack or Queue; or you can write a recursive method. { //Stack stk declared and used already ... int theSize;

Name:

CS 112 Midterm #1, Fall 1998

6. (14 points) Queues implemented as wrapped arrays. Here is the relevant part of the implementation of QueueAr in the textbook: class QueueAr ... { private Object[] theArray; private int currentSize; private int front = 0; //initially, 0 private int back = -1; //initially, -1 ... In each of the following cases, complete as best you can the description of currentSize in terms of front, back and theArray.length. (a) (b) (c) (d) if if if if front==back then currentSize == front<back then currentSize == front>back+1 then currentSize == front==back+1 then currentSize ==

7. Implemented as Linked Structures. Here is the relevant part of the implementation of queues as linked lists class QueueLi ... { private LinkedNode front; private LinkedNode back; ... Suppose we want to add a method append(Queue other) so that if x and y are queues then x.append(y); results in x having added to its end the values in y. For example, if x was ham , egg , bread and y was oil , wine , then x.append(y) would be ham , egg , bread , oil , wine . (a) Complete the method below on class QueueLi, so append runs in constant time, O(1). public void append(QueueLi other) { //tacks on other to the end of this queue

(b) Suppose the queues x and y had the contents described in the above example. The following questions refer to your constant-time implementation of append, which you just gave. i. What would be the contents of x after the following sequence of statements

x.append(y); y.enqueue("dog");
x contains ii. What would be the contents of y after the following sequence of statements

x.append(y); x.enqueue("dog");
y contains iii. What would be the contents of both x and y after the following sequence of statements

x.append(y); y.makeEmpty(); y.enqueue("dog");


x contains y contains

Name:

CS 112 Midterm #1, Fall 1998

Method headers from the assignment


public class OrderedList { private Object info; private OrderedList subList; public OrderedList() ; private OrderedList(Object x, OrderedList r); public Object find(Comparable x) ; public void add(Comparable x) ; public String toString() ; public Enumeration getEnumeration(); class Enum implements Enumeration { private OrderedList enum; private Enum(OrderedList sll); public boolean hasMoreElements(); public Object nextElement() throws NoSuchElementException; } public static void main( String [ ] args ) { private void testOne(); private void testTwo(); class TestObj implements Comparable { int data; TestObj(int x); public int compares(Comparable x); public boolean lessThan(Comparable x); public String toString(); } } public class CrossReferenceEntry implements Comparable { private OrderedList lines; private String identifier; public CrossReferenceEntry(String id, int ln); public int compares(Comparable x); public boolean lessThan(Comparable x); public void add(int ln); public String toString(); class CREI implements Comparable; int data; CREI(int x); public int compares(Comparable x); public boolean lessThan(Comparable x); public String toString(); } } public class CrossReferenceTable { OrderedList table; public CrossReferenceTable(); public void add(String id, int ln); public String toString(); }

Name: This page was left intentionally blank

CS 112 Midterm #1, Fall 1998

Das könnte Ihnen auch gefallen