Test 2 Solutions - Program Design and Development - Summer 2006 | C SC 227, Exams of Computer Science

Material Type: Exam; Class: Full Course Title: Program Design and Development; Subject: COMPUTER SCIENCE; University: University of Arizona; Term: Summer 2006;

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-dhq
koofers-user-dhq 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Answers to C Sc 227 Practice Test #2, Summer 2006
1.
last = new Node("B");
last.next = new Node("A", last);
2.
// Code could have been written like his also
Node first = new Node("A");
first.next = new Node("B");
"A"
"B"
first
3.
public void addFirst(E element) {
first = new LinkNode(element, first);
}
4.
public void addLast(int index, E element) {
if (front == null)
first = new Node(element);
else { // General case
Node ref = first;
while (ref.next != null) {
ref = ref.next;
}
ref.next = new Node(element);
}
}
5.
public void size() {
int result = 0;
LinkNode ref = first;
while(ref != null) {
ref = ref.next;
result++;
}
return result;
}
6. In-class
7.
1 A B 1 A C 1 A B
2 A B 2 A C
3 A B
8.
0
X<<<2
X<<4
X<6
pf3
pf4

Partial preview of the text

Download Test 2 Solutions - Program Design and Development - Summer 2006 | C SC 227 and more Exams Computer Science in PDF only on Docsity!

Answers to C Sc 227 Practice Test #2, Summer 2006

last = new Node("B"); last.next = new Node("A", last);

// Code could have been written like his also Node first = new Node("A"); first.next = new Node("B"); "A" (^) "B" first

public void addFirst (E element) { first = new LinkNode(element, first); }

public void addLast(int index, E element) { if (front == null) first = new Node(element); else { // General case Node ref = first; while (ref.next != null) { ref = ref.next; } ref.next = new Node(element); } }

public void size () { int result = 0; LinkNode ref = first; while(ref != null) { ref = ref.next; result++; } return result; }

  1. In-class
  2. 1 A B 1 A C 1 A B 2 A B 2 A C 3 A B
  3. 0 X<<< X<< X<
  1. 1 mystery(0) 4 mystery(1) 7 mystery(2) 10 mystery(3) 13 mystery(4)
  2. /T mysteryTwo("T") /b/a mysteryTwo("ab") 1/2/3 mysteryTwo("123")
  3. stars(1) ***** stars(2) *****
    stars(3) *****

  4. In-class

private int sum(Node n) { if(n!=null) return n.data + sum(n.next); else return 0; }

  1. The public method should have had an int argument public boolean contains(int value) { return contains(first, value); } private boolean contains(Node n, int value) { if(n != null) { if(n.data == value) return true; else return contains(n.next, value); } else return false; }
  2. The public method should have had an int argument public int occurencesOf( int i ) { return occurencesOf(first, i); } private int occurencesOf(Node ref, int i) { int count = 0; if (ref == null) return 0; if (ref.data == i) count = 1; return count + occurencesOf(ref.next, i); }
  3. A B D C E F B D A E C F

private K maxKey(MapNode t) { if (t.right == null) return t.key; else { return maxKey(t.right); } }

public String toString() { return toString(root, ""); } private String toString(MapNode t, String result) { if (t == null) return ""; else return toString(t.left, result)

  • t.key + " " + toString(t.right, result); }
  1. In class
  2. In class
  3. In class

Sorted Array O(log n)

Unsorted Array O(n)

Singly Linked Structure O(n)

Binary Tree O(n)

Binary Search Tree O(log n)