Docsity

CIS 203 Midterm I: Questions on Chapters 1-5.4 - Prof. Susan Haller, Exams of Computer Science

Toggle sidebar
bg1
Name CIS 203 Midterm I: Chapters 1-5.4
October 3, 2007
Directions: This is a closed book, closed notes examination. Place your answers in the space provided. The point
value of each question is indicated. There are a total of xx points. Your total out of 63 will be weighted to 100
points. You have 55 minutes for this examination.
1. (9 pts ) Consider the following code segments. What is the runtime complexity in Big-O notation as a function of
the size of the input n?
a) for (int i = 0; i < n ; i++)
int x = 5;
for (int i = 0; i < 2*n; i++)
int x = 7;
b) for (int i = n; i > 0; i--) {
int x = 5;
for (int j = 0; j < 2 * n; j++)
int y = 7;
}
c) for (int i = n; n > 0; i++)
n = n / 3;
1f07
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CIS 203 Midterm I: Questions on Chapters 1-5.4 - Prof. Susan Haller and more Exams Computer Science in PDF only on Docsity!

Name CIS 203 Midterm I: Chapters 15. October 3, 2007 Directions: This is a closed book, closed notes examination. Place your answers in the space provided. The point value of each question is indicated. There are a total of xx points. Your total out of 63 will be weighted to 100 points. You have 55 minutes for this examination.

  1. (9 pts ) Consider the following code segments. What is the runtime complexity in BigO notation as a function of the size of the input n? a) for (int i = 0; i < n ; i++) int x = 5; for (int i = 0; i < 2*n; i++) int x = 7; b) for (int i = n; i > 0; i) { int x = 5; for (int j = 0; j < 2 * n; j++) int y = 7; } c) for (int i = n; n > 0; i++) n = n / 3;
  1. (9 pts) Consider the following class definition: import java.awt.*; public class Circle implements Cloneable { // data members private Point center; private int radius; // constructor public Circle (int x, int y, int r) { center = new Point(x, y); radius = r; } // clone public Object clone() { Circle copy; try { copy = (Circle) super.clone(); } catch (CloneNotSupportedException e) { throw new RuntimeException("This class does “ + “not implement Cloneable"); } return copy; } // moveCenter public void moveCenter(int x, int y) { center.setLocation(x,y); } // toString public String toString() { return "Circle with radius " + radius + " and center at " + "(" + center.getX() + ", " + center.getY() + ")"; } } Also consider the following client code segment: ... Circle c1 = new Circle(0, 0, 5); Circle c2 = (Circle) c1.clone(); c2.moveCenter(6, 6); System.out.println(c1); System.out.println(c2);
  1. (9 pts) Consider the stripped down version of the DoubleArraySeq class in Appendix B. a) Implement the start() method. This method sets the current element to be the front of this sequence. The postcondition is that the front element of the sequence is now the current element. If this sequence has no elements at all, then there is no current element. b) Implement the addBefore() method. The parameter is a new element being added to the sequence. The postcondition is that the new element is added before the current element and becomes the current element. If there is no current element, the new element is placed at the beginning of the sequence and becomes the current element. If this new element would take the sequence beyond its current capacity, the capacity is increased first.
  1. (9) Consider a linked list of IntNodes. a) Describe a nondestructive strategy for reversing the list. b) What does the following method do to a linked list of IntNodes? Is it nondestructive? public static IntNode mystery (IntNode head) { IntNode x = null; IntNode y; IntNode z = head; while (z != null) { y = z; z = z.getLink(); y.setLink(x); x = y; } return x; }
  1. (9 pts) Consider the following class definition. public class Stuff { int [] data; int numberOfItems; public Stuff (int size) { data = new int [size]; numberOfItems = 0; } public void add(int item) { if (numberOfItems == data.length) return; // no room to add data[numberOfItems++] = item; } public int get (int index) { return data[index]; } public static Stuff combine (Stuff s1, Stuff s2) { Stuff combinedStuff = new Stuff(s1.data.length + s2.data.length); for (int i = 0; i < s1.numberOfItems; i++) combinedStuff.data[i] = s1.data[i]; for (int i = s1.numberOfItems; i < s1.numberOfItems + s2.numberOfItems; i++) combinedStuff.data[i] = s2.data[i – s1.numberOfItems]; combinedStuff.numberOfItems = s1.numberOfItems + s2.numberOfItems; return combinedStuff; } } Rewrite this class converting it to a generic class. Use the next page or clearly and neatly indicate your changes on the code above.

Midterm Appendix B

public class DoubleArraySeq implements Cloneable { // data members private double[ ] data; private int manyItems; private int currentIndex; // constructor public DoubleArraySeq( ) { data = new double[10]; manyItems = 0; currentIndex = 01; } // addBefore public void addBefore(int element) { // Implemented by student. } // advance public void advance( ) { currentIndex++; } // ensureCapacity public void ensureCapacity(int minimumCapacity) { if (getCapacity() >= minimumCapacity) return; double[] newData = new double[minimumCapacity]; for (int i = 0; i < manyItems; i++) newData[i] = data[i]; data = newData; } // getCapacity public int getCapacity( ) { return data.length(); } // isCurrent public boolean isCurrent( ) { return currentIndex >=0 && currentIndex < manyItems; } // size public int size( ) { return manyItems; } // start public void start( ) { // Implemented by student. } }