cs112mid1-1999(rutgers)

8
CS 112 Fall 1998 Midterm Exam #1 Your name: Circle instructor: borgida/iacono/morris/smith Your student number: TA’s 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 efficiently. You may not be able to finish 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 Score 1 30 2 26 3 26 4 16 5 18 6 14 7 20 Total 150 1

Upload: manan-shah

Post on 28-Oct-2015

184 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS112mid1-1999(RUTGERS)

CS 112 Fall 1998

Midterm Exam #1

Your name: Circle instructor: borgida/iacono/morris/smith

Your student number: TA’s 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 0on the exam if you fail to turn in the exam when asked to do so.

• Use your time efficiently. You may not be able to finish every problem in the time allowed.

• If you are taking this exam to make up a grade for a previous semester, please explain yourcircumstances below.

• A list of classes and methods from the project is provided at the end of the exam.

Problem Possible Score1 302 263 264 165 186 147 20

Total 150

1

Page 2: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 2

1. (30 points) On complexity

Evaluating a polynomial of the form f(y) = a0 +a1 ∗ y1 +a2 ∗ y2 + . . .+an ∗ yn can be done usingthe following algorithm, if the coefficients are stored in an array a, and the value for the argumentis 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 abound as possible.)

(c) Use the fact that yn = y ∗ y(n−1) to improve the previous algorithm so that its complexityorder 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?

Page 3: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 3

Questions 2 and 3 below refer to Assignment 1. For your reference, the complete list of methodsfor it appears at the end of the exam.

2. (26 points) In the class CrossReferenceEntry from Assignment 1, the method add adds a linenumber to an existing entry. Each use of an identifier is recorded resulting in the possibility ofthe same line number occuring multiple times for one identifier. Provide below an add method forthe class CrossReferenceEntry that records each line on which an identifier is found but doesNOT 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 poorestbased 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. Besure that that when inserted into an OrderedList the result will be ordered from wealthiest topoorest. Grades will be based on correctness, efficiency, simplicity, and effective 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;

Page 4: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 4

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) {1 LinkedNode q = p.next2 p.next = q.next;3 LinkedNode r = p.next.next;4 q.next.next = q;5 p.next.next.next = r

}

You will be asked to trace the code above, line by line, by completing the diagrams with arrowsfor 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:

a b d c

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

a b d c

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

a b d c

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

a b d c

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

a b d c

Page 5: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 5

5. (18 points) Using data structures.

The following are interfaces for Stack and Queue, indicating the functions available to you.

public class Stack public class Queue{ {Stack(); Queue();void push(Object); void enqueue(Object);Object pop(); //topAndPop from the text Object dequeue();boolean isEmpty(); boolean isEmpty();void makeEmpty(); void makeEmpty();

} }

Given some a variable, stk of type Stack, you are required to compute its size. You do not knowthe internal representation of the stack; size is not a method of Stack; and at the end, stk mustbehave no differently 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 Stackor Queue; or you can write a recursive method.

{ //Stack stk declared and used already...int theSize;

}

Page 6: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 6

6. (14 points) Queues implemented as wrapped arrays. Here is the relevant part of the imple-mentation of QueueAr in the textbook:

class QueueAr ... {private Object[] theArray;private int currentSize;private int front = 0; //initially, 0private int back = -1; //initially, -1...

In each of the following cases, complete as best you can the description of currentSize in termsof front, back and theArray.length.

(a) if front==back then currentSize ==

(b) if front<back then currentSize ==

(c) if front>back+1 then currentSize ==

(d) if front==back+1 then currentSize ==

7. Implemented as Linked Structures. Here is the relevant part of the implementation of queuesas 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 thenx.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 followingquestions 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 statementsx.append(y); y.enqueue("dog");x contains

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

y containsiii. 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 containsy contains

Page 7: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 7

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

}

Page 8: CS112mid1-1999(RUTGERS)

Name: CS 112 Midterm #1, Fall 1998 8

This page was left intentionally blank