Download Resolution for Test 2 - Program Design and Development - 2009 | C SC 227 and more Exams Computer Science in PDF only on Docsity!
C Sc 227 Spring 09 Practice Test 2 Section Leader _______ Name _________________________ 100pts
1. Determine the tightest upper bound runtimes of the following loops. Express your answer in the Big-O notation we
have been using in class (assume the initialization int sum = 0;). 15pts
a. ___________ for (int j = n; j >= 0; j--) sum++;
b. ____________ for (int j = 1; j <= n; j = j + 2) sum++;
c. ____________ for(int k = 1; k <= n; k = 2 * k) sum++;
d. ___________ for( int j = 1; j <= n; j++ ) for( int k = 1; k <= n; k++ ) sum++; for(int m = 1; m <= n; m++ ) sum++;
e. ____________ int j = 1; sum++;
2. Write an X in the comment to the right of all statements that compile. (4pts)
Object anObject = new Integer(3); // a. ____ Object otherObject = "a string"; // b. ____ Integer anInt = new Object(); // c. ____ Double d = new Object(); // d. ____
3. Write an X in the comment to the right of all statements that compile. (4pts)
Integer i1 = 3; // a. ____ int i2 = new Integer(-1); // b. ____ Object obj = 3; // c. ____ Double d3 = 12; // d. ____
4. Using the definitions in the book, for each item, write DS if it is a data structure, COLL if it is a collection
class or ADT if it is abstract data type. (6pts)
a._______ 1D array b._________ PriorityList c._______ ArrayPriorityList
5. Write the output generated by the following code (10 pts)
int[] a = { 1, 5, 2, 4, 3, 7 }; int n = a.length;
for (int i = 0; i < n; i++) { for (int inner = i; inner < n - 1; inner++) { if (a[inner] < a[inner + 1]) { int temp = a[inner]; a[inner] = a[inner + 1]; a[inner + 1] = temp; } } }
for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); }
6. What is the Big O runtime of the code in above question (5)? (3pts)
first
"one" "two" "three"
7. Change class BankAccount so it can be sorted using compareTo and the natural ordering is the difference in
pennies. The following assertions must compile and pass with the values shown. The first assertion shows
that the BankAccount with ID "A" has 25 pennies less than the BankAccount with ID "B". The class must
implement interface Comparable (8pts)
public interface Comparable { // Compares this object with the specified object for order. // Returns a negative integer, zero, or a positive integer as this object // is less than, equal to, or greater than the specified object. public int compareTo(T other) }
/////////////////////////////// import static org.junit.Assert.*; import org.junit.Test;
public class TestBankAccountCompare { @Test public void testComparePennies() { assertEquals(-25, new BankAccount("A", 1.00).compareTo(new BankAccount("B", 1.25))); assertEquals( 25, new BankAccount("B", 1.25).compareTo(new BankAccount("D", 1.00))); assertEquals( 0, new BankAccount("C", 1.25).compareTo(new BankAccount("E", 1.25))); } }
/////////////////////////////////////////////////////////////////// // A class that model a very simple BankAccount that can be sorted public class BankAccount { private String my_ID; private double my_balance;
public BankAccount(String initID, double initBalance) { my_ID = initID; my_balance = initBalance; }
8. Using the familiar Node class (shown on the next page) and this
view of a linked structure to answer the questions that follow (10pts)
a. What is the value of first.data? _______
b. What is the value of first.next.next.data? _____________
c. What reference value equals first.next.next.next? _____________
d. Write the code that prints all values. Make the code general enough to work on any sized linked structure where
the last node references the first (this is a circular linked list)
11. To the same class LinkedList above, add method reverse() that reverses the ordering of elements. The
following assertions should pass with a correct toString method. (12pts)
@Test public void testReverse() { LinkedList list = new LinkedList(); list.addFirst("A"); list.addFirst("B"); list.addFirst("C"); assertEquals("[C, B, A]", list.toString()); list. reverse() ; assertEquals("[A, B, C]", list.toString()); list. reverse() ; assertEquals("[C, B, A]", list.toString()); }
// Add the reverse method here (12pts)
public void reverse() {
Use the following interfaces for stack and queue abstract data types on the next page.
public interface OurStack {
// Check if the stack is empty to help avoid popping an empty stack. public boolean isEmpty ();
// Put element on "top" of this Stack object. public void push (E element);
// Return reference to the element at the top of this stack. public E peek () throws EmptyStackException;
// Remove element at top of stack and return a reference to it. public E pop () throws EmptyStackException;
}
public interface OurQueue { // Return true if this queue has 0 elements public boolean isEmpty ();
// Store a reference to any object at the end public void add (E newEl);
// Return a reference to the object at the // front of this queue (you may need to cast) public E peek ();
// Remove a reference to the element at the front of this Queue and return that reference public E remove ();
}
12. Write the output generated by the following code. (6pts)
OurStack s = new LinkedStack();
s.push('3');
s.push('2');
System. out .println(s.peek());
System. out .println(s.pop());
System. out .println(s.isEmpty());
13. Write the output generated by the following code (6pts)
OurQueue q = new LinkedQueue();
q.enqueue("a");
q.enqueue("b");
q.enqueue("c");
while (! q.isEmpty()) {
System. out .print(q.dequeue() + " ");
14. Complete method doubleQueue that will cause every element in the LinkedQueue argument to be duplicated. If
the LinkedQueue contains the objects "a", "b", "c", "d", "e" (with "a" at the front of the queue and "e" at the end) the
LinkedQueue should be made to contain "a", "a", "b", "b", "c", "c", "d", "d", "e", "e" (with "e" still at the end). The
LinkedQueue referenced by q could have any number of elements in it. The given assertion must pass. (8pts)
GenericQueue q = new LinkedQueue();
q.add("a");
q.add("b");
q.add("c");
doubleQueue(q);
assertEquals( "a" , q.remove());
assertEquals( "a" , q.remove());
assertEquals( "b" , q.remove());
assertEquals( "b" , q.remove());
assertEquals( "c" , q.remove());
assertEquals( "c" , q.remove());
assertTrue(q.isEmpty());
// Complete this method
public void doubleQueue (LinkedQueue q) {