C Sc 227 Practice Test 2 Section Leader Your Name 100pts 1. Approximately how many lectures remain in C Sc 227 (give or take 2)? (2pts) 2. 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;). 6pts (1pt ea, 2pts for overall style) a. for (int j = n; j >= 0; j--) b. for (int j = 1; j <= 50 * n; j++) c. for(int k = 1; k <= n; k = 2 * k) d. for( int j = 1; j <= n; j++ ) for( int k = 1; k <= n; k++ ) for(int m = 1; m <= n; m++ ) e. int j = 1; 3. Using the definitions from Rick's book, for each item, write DS if the name refers to a data structure, COLL if a collection class or ADT if an abstract data type. (3pts) a. 1D array b. PriorityList<E> c. ArrayPriorityList<E> 4. Use the Node class shown below with data and next instance variables and this view of a linked structure to answer the questions a) through d) (7pts) first "one" "two" "three" 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) 5. Write Java code that will generate the following linked structure assuming you have access to the Node class shown below. (3pts) back "A" "B" 1
6. To class LinkedList, write method reverse() so all elements are stored in reverse order. Also write method duplicateall(e) that adds another element that equals the argument next to every occurrence of that argument. The code in the box must compile with passing assertions. This version of LinkedList only has the constructor plus the three methods shown as stubs addlast(e), tostring(), and size().you cannot pretend there are other LinkedList methods. If you need another method, 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 size; public LinkedList() { first = null; size = 0; public int size() { return n; public String tostring() { //... public void addlast () { //... public void reverse() { // 10pts @Test public void testreverse() { LinkedList<String> list = new LinkedList<String>(); list.addlast("a"); list.addlast("b"); list.addlast("c"); assertequals("[a, B, C]", list.tostring()); list.reverse(); assertequals("[c, B, A]", list.tostring()); @Test public void testduplicateall() { LinkedListOld<Integer> list = new LinkedListOld<Integer>(); list.addlast(1); list.addlast(2); list.addlast(3); list.addlast(2); assertequals(4, list.size()); assertequals("[1, 2, 3, 2]", list.tostring()); list.duplicateall(2); assertequals("[1, 2, 2, 3, 2, 2]", list.tostring()); assertequals(6, list.size()); Write duplicateall on the next page 2
public void duplicateall(e value) { // 12pts Use the following interface for the two Stack questions that follow: public interface OurStack<E> { public boolean isempty(); public void push(e element); public E peek(); public E pop(); 7. Write the output generated by the following code: (4pts) OurStack<Character> s = new LinkedStack<Character>(); s.push('a'); s.push('b'); s.push('c'); System.out.println(s.peek()); System.out.println(s.pop()); System.out.println(s.isEmpty()); System.out.println(s.peek()); 8. Complete method popuntil to modify the stack argument until value is found or the stack is empty. Do not allow popuntil to throw any exceptions if value is not in the stack argument. (6pts) OurStack<String> s = new LinkedStack<String>(); s.push("1"); s.push("+"); s.push("("); s.push("2"); popuntil("(", s); assertequals("+", s.pop()); assertequals("1", s.pop()); public void popuntil(string value, OurStack<String> s) { 3
9. Write the return values from each call to the method named mystery. (4pts) mystery(25) mystery(19) mystery(9) public int mystery(int n) { if (n > 20) return 1; else if (n > 10) return 5 + mystery(n + 5); else return 3 + mystery(n + 8); 10. Write the output generated by the method call mystery2("x", 6); (3pts) 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); 11. Implement recursive method trib. Use recursion, do not use a loop. (4pts) 12. Given a non-negative int n, compute recursively (no loops) the count of the occurrences of 8 as a digit, except that an 8 with another 8 immediately to its left counts double, so 8818 yields 4. The following assertions must pass. (12pts) assertequals(1, count8(8)); assertequals(2, count8(818)); assertequals(3, count8(88)); assertequals(3, count8(881)); assertequals(3, count8(188)); assertequals(9, count8(88888)); public int count8(int n) { 4
13. 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. (12pts) odddownevenup(1); // 1 odddownevenup(2); // 1 2 odddownevenup(3); // 3 1 2 odddownevenup(4); // 3 1 2 4 odddownevenup(5); // 5 3 1 2 4 odddownevenup(9); // 9 7 5 3 1 2 4 6 8 public void odddownevenup(int n) 14. Complete method occurrencesof that returns how often an int element occurs in an array on integer. First complete this test method (fill in the four blanks). (12pts) int[] a = { 1, 2, 1, 2, 4 ; assertequals(, occurrencesof(1, a); assertequals(, occurrencesof(2, a); assertequals(, occurrencesof(4, a); assertequals(, occurrencesof(999, a); public int occurrencesof(int value, int[] x) { 5