C Sc 227 Practice Final Name

C Sc 227 Practice Final
Name_________________________________
1. Is it currently raining in Tucson ______ (4pts)
a) Yes
b) No?
c) Don't know
d) Couldn't know (not in Tucson)
2. Use our familiar Node class (shown below with data and next instance variables) and this view of a linked structure to
answer the questions a) through d)
a)
b)
c)
d)
What is the value of first.data? _______ (1pts)
What is the value of first.next.next.data? _______ (2pts)
What reference value equals first.next.next.next? __________ (2pts)
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) (5pts)
3. Write Java code that will generate the following linked structure assuming you have access to the familiar Node class.
(4pts)
4. On the next page, complete a) method reverse() so all elements are stored in reverse order. Also complete b) method
addLast(E) to add elements to the end of the LinkedList. The code in the box must compile with passing assertions,
and of course both methods must work in all cases. You cannot assume there are any other LinkedList methods. If you
need another method, you will have to write it.
public class LinkedList<E> {
private class Node {
private E data;
private Node next;
public Node(E objectReference) {
data = objectReference;
}
public Node(E objectReference,
Node nextReference) {
data = objectReference;
next = nextReference;
}
}
private Node first;
private int n;
public LinkedList() {
1
first = null;
n = 0;
}
public void addFirst(E element) {
first = new Node(element, first);
n++;
}
public int size() {
return n;
}
public String toString() {
if (first == null)
return "[]";
else if (first.next == null)
return "[" + first.data + "]";
else {
String result = "[";
Node ref = first;
while (ref.next != null) {
result += ref.data + ", ";
ref = ref.next;
}
return result + ref.data + "]";
}
}
@Test
public void testReverse() {
LinkedList<String> list = new LinkedList<String>();
list.addFirst("A");
list.addFirst("B");
list.addFirst("C");
assertEquals("[C, B, A]", list.toString());
list.reverse();
assertEquals("[A, B, C]", list.toString());
}
@Test
public void testAddLast() {
LinkedList<String> list = new LinkedList<String>();
list.addLast("A");
list.addLast("B");
list.addLast("C");
list.addLast("D");
assertEquals(4, list.size()); // Is size correct?
assertEquals("[A, B, C, D]", list.toString());
}
// a) Complete the reverse method here
public void reverse() {
// 12 pts
// b) Write the addLast method here
public void addLast(E element) {
// 12pts
2
5. Write the output generated by the following code. (4pts)
OurStack<Character> s = new LinkedStack<Character>();
s.push('x');
s.push('y');
System.out.println(s.peek());
System.out.println(s.pop());
System.out.println(s.isEmpty());
6. Complete method doubleStack that will cause every element in the LinkedStack argument to be duplicated. If the
LinkedStack contains the objects "a", "b", "c" (with "c" at the top of the stack) the LinkedStack should be made to
contain "a", "a", "b", "b", "c", "c" (with the "c"s still at the top). The object referenced by s could have any number of
elements in it. These assertions must pass. (12pts)
OurStack<String> s = new LinkedStack<String>();
s.push("a");
s.push("b");
s.push("c");
doubleStack(s);
assertEquals("c", s.pop());
assertEquals("c", s.pop());
assertEquals("b", s.pop());
assertEquals("b", s.pop());
assertEquals("a", s.pop());
assertEquals("a", s.pop());
assertTrue(s.isEmpty());
// Complete this method
public void doubleStack(OurStack<String> arg) {
3
7. Write the return values from each call to the method named mystery.
____mystery(0)
public int
if (n <=
return
else
return
}
____mystery(1)
____mystery(2)
(8pts)
____mystery(3)
____mystery(4)
mystery(int n) {
0)
1;
stars(1)
3 + mystery(n - 1);
___________________________________
8. In the box to the right , write the output of stars with of arguments
1, 2, and 3. (5pts)
stars(2)
___________________________________
public static void stars(int n) {
if (n > 1)
stars(n-1);
for (int i = 0; i < n; i++)
System.out.print("*");
System.out.println();
}
stars(3)
9. Write the output generated by the method call mystery2("X", 6); (7pts) __________
public void mystery2(String s, int digit) {
if(digit <= 1)
System.out.println(digit);
else {
s = s + "<";
mystery2(s, digit - 2);
System.out.println(s + digit);
}
}
10. Given a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been changed to 'y'
chars. Use recursion. Do not use a loop. (10pts)
changeXY("codex") → "codey"
changeXY("xxhixx") → "yyhiyy"
changeXY("xhixhix") → "yhiyhiy
4
11. Use recursion to complete method oddDownEvenUp so it first prints all odd integers from the largest odd number <=
argument down to 1. The same method call must then print all the even integers in the range of 2 through the largest even
integers <= argument. The following method calls shown to the left must generate the output shown in comments to the
right. Do not use any loop anywhere, use recursion. (16pts)
oddDownEvenUp(1);
oddDownEvenUp(2);
oddDownEvenUp(3);
oddDownEvenUp(4);
oddDownEvenUp(5);
oddDownEvenUp(9);
//
//
//
//
//
//
1
1
3
3
5
9
2
1
1
3
7
2
2 4
1 2 4
5 3 1 2 4 6 8
public void oddDownEvenUp(int n)
12. Use the following binary tree to write out each of the three traversals indicated below. (12pts)
5
Preorder traversal __________________________________________________
Inorder traversal
__________________________________________________
Postorder traversal __________________________________________________
13. Is this a Binary Search Tree assuming only String keys are shown? (yes or no)? _______
(3pts)
root
14. Is this a Binary Search Tree assuming only String keys are shown? (yes or no)? _______
(3pts)
root
15. Draw a picture of the data that results from the following code on your BinarySearchTree class. Be sure to have the
instance variable root reference the root node with an arrow. Show values. (8pts)
BinarySearchTree<String> aBST = new BinarySearchTree<String>();
aBST.insert("Matrix");
aBST.insert("Antz");
aBST.insert("Bean");
aBST.insert("Shaft");
aBST.insert("Scream");
aBST.insert("Titanic");
16. Complete the output that would be generated by this code. (8pts)
ArrayList<Integer> list = new ArrayList<Integer>();
6
list.add(12);
list.add(-5);
list.add(23);
list.add(-5);
Collections.sort(list);
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
System.out.println("\n===");
System.out.println(list.contains(-5));
System.out.println(list.contains(9999));
System.out.println(Collections.min(list));
System.out.println(Collections.max(list));
A fewArrayList methods:
//
17. Complete the output that would be generated by this code. (6pts)
How many elements currently in this Set
public int size();
HashMap<Integer, String> map = new HashMap<Integer, String>();
// Add element to this set only if element does
map.put(50, "M");
// not equal another element already in this Set
map.put(25, "B");
public boolean add(E element);
map.put(50, "C");
map.put(75, "D");
// Return true if element is in this Set
System.out.println(map.size());
public boolean contains(E element);
System.out.println(map.get(25));
System.out.println(map.get(50));
// Return the element at the given index so we can
System.out.println(map.get(75));
// access all elements in the set using a for loop
System.out.println(map.get(99));
public E get(int index);
18. Complete intersect so it returns a List that only has any and all elements that are contained in both List<Integer>
arguments. (12pts)
@Test
public void testIntersect() {
List<Integer> ts1 = new ArrayList<Integer>();
ts1.add(2);
ts1.add(4);
ts1.add(2);
ts1.add(4);
List<Integer> ts2 = new LinkedList<Integer>();
ts2.add(2);
ts2.add(2);
ts2.add(4);
ts2.add(6);
ts2.add(4);
// intersection([2, 4, 2, 4], [2, 2, 4, 6, 4]) should return [2, 4] or [4, 2]
List<Integer> result = intersection(ts1, ts2);
assertEquals(2, list.size());
assertTrue(list.contains(2));
assertTrue(list.contains(4));
}
public List<Integer> intersect (List<Integer> s1, List<Integer> s2) {
7
19. Complete method maxKey in BinarySearchTree that returns a reference to the key with the maximum value. Write
two different implementations of a private helper method. One method must have a recursive solution. The other method
will have an iterative solution. These assertions should pass:
@Test public void testMaxKey() {
BinarySearchTree<Integer > intTree = new BinarySearchTree<Integer>();
assertNull(intTree.maxKey());
intTree.insert(5);
assertEquals(new Integer(5), intTree.maxKey());
intTree.insert(8);
assertEquals(new Integer(8), intTree.maxKey());
intTree.insert(-3);
assertEquals(new Integer(8), intTree.maxKey());
}
///////////////////////////////////////////////////////////
public class BinarySearchTree<E extends Comparable<E>> {
private class TreeNode {
private TreeNode left;
private TreeNode right;
private E data;
public TreeNode(K element) {
key = theKey;
value = theValue;
left = null;
right = null;
}
} // end class TreeNode
private TreeNode root;
private int size;
// Create an empty tree
public BinarySearchTree() {
root = null;
size = 0;
}
a) Write the public and private methods using a recursive solution.
8
(8pts)
b) Write the complete public method with an iterative solution. (8pts)
20. To the same BinarySearchTree class above, add method List<E> valuesGreaterThan(E key) to return a
List<Integer> of E in the BinarySearchTree object that are greater than the argument key. The given
assertions must pass. (12pts)
@Test public void testKeysGreater() {
BinarySearchTree<Integer> om = new BinarySearchTree<Integer>();
om.insert(50);
om.insert(60);
om.insert(45);
om.insert(55);
Set<Integer> set = om.valuesGreaterThan(50);
assertTrue(set.contains(55));
assertTrue(set.contains(60));
assertFalse(set.contains(45));
assertFalse(set.contains(50));
}
// Complete this method
public List<Integer> valuesGreaterThan(Integer value) {
9
21. To the same BinarySearchTree class, add method reverse() that changes the ordering property of a BST such
that an inOrder traversal would visit all elements in descending order. Empty tree and trees with only one node will not
change. Trees with two or more nodes will change. (16pts)
root
\
M
/
G
root
\
99
/ \
12
555
root
\
M
/ \
G
S
/ \
P
V
becomes
root
\
M
\
G
becomes
becomes
root
\
99
/ \
555
12
root
\
M
/ \
S
G
/ \
V
P
public void reverse() {
10