UNIVERSITY OF VICTORIA EXAMINATIONS APRIL 1998 COMPUTER SCIENCE 115/160 (S01) NAME: I.M. Solution INSTRUCTOR: Frank Ruskey . REG. NO. SECTION: S01 DURATION: 3 Hours TO BE ANSWERED ON THE PAPER. STUDENTS MUST COUNT THE NUMBER OF PAGES IN THIS EXAMINATION PAPER BEFORE BEGINNING TO WRITE, AND REPORT ANY DISCREPANCY IMMEDIATELY TO THE INVIGILATOR. THIS QUESTION PAPER HAS 10 PAGES. NOTES: (1) ANSWER ALL QUESTIONS, (2) THERE ARE A TOTAL OF 90 MARKS, (3) YOU ARE ALLOWED A 1 PAGE ”CHEAT-SHEET” BUT NO OTHER MATERIALS. SCRATCH PAPER IS AVAILABLE FROM THE INVIGILATOR. Question Possible marks Actual marks 1 20 2 3 3 5 4 5 5 5 6 3 7 5 8 5 9 4 10 4 11 4 3+3=6 12 8 13 10 14 5 15 4 Total 90 92 CSC 115/160 (S01), 2 1. Fill in the blanks: [1 mark each (22), 20 marks total possible] (a) What’s another name for a LIFO (last-in first-out list)? ANSWER: a stack. (b) In Java, a program (e.g., a project in Metroworks Codewarrior) running out of a WWW browser is called an ANSWER: applet, otherwise it is an ANSWER: application. (c) In Java, is there any limit on the number of superclasses that a class can have? If so, what is it? ANSWER: yes, 1. Every class, except “Object” has exactly one superclass. (d) In Java, is there any limit on the number of subclasses that a class can have? If so, what is is? ANSWER: no, a class have have many subclasses (e) In Java we don’t have to worry about objects that have been created but that are no longer referenced by our program. Why not? ANSWER: Because Java has a “garbage collector” (f) In Java, if you wanted to work with an array-like data structure (allowing indexing) whose size could change dynamically (e.g., by repeated insertions), what class in the API would you use? ANSWER: Vector. (g) Write a Java expression that is true if and only if the object obj is an instance of the class myClass. ANSWER: obj instanceof myClass. (h) Write a Java expression that is true if and only if the last three bits in the binary representation of an integer n are all 1’s. ANSWER: n & 7 == 7 (i) What is 0 + 1 + 2 + · · · + 2n−1 ? (simplify) ANSWER: 2n − 1 (j) Can a Java interface have a constructor? Briefly explain your answer. ANSWER: NO, interfaces are never instantiated. (k) Can an abstract class be instantiated? Briefly explain your answer. ANSWER: NO, abstract classes can never be instantiated since their methods have no bodies. (l) Name an essential feature of C that is not present in Java. ANSWER: explicit pointers, malloc, the preprocessor, etc. (m) Name a feature of Java that is not present in C++ or C. ANSWER: the garbage collector, etc. (n) Name a feature of C++ that is not present in Java or C. ANSWER: destructors, operator overloading, etc. (o) Give the best (i.e., with slowest growing function) big-O expression for the running time of heapsort of n items.. ANSWER: O(n log n)). (p) Give the best big-O expression for the running time of insertionsort of n items. ANSWER: O(n2 ). (q) Give the best big-O expression for the running time of finding the largest of n elements in an unsorted array. ANSWER: O(n). (r) Name a primitive data type in Java. ANSWER: double (lots of other correct answers). (s) Name a reference data type in java.lang. ANSWER: System, String, etc. (t) Name an advantage of using a sentinel in a search method. ANSWER: simpler code (or faster execution). (u) If algorithm A is O(n) and algorithm B is O(n2 ), then is algorithm A guaranteed to be faster than algorithm B whenever n is large enough? Explain briefly. ANSWER: NO, for example, the true √ running time of algorithm B could be n and the true running time of algorithm A could be n. CSC 115/160 (S01), 3 2. In the class String there is a method concat that has a parameter that is a String and that returns a String. The following code is meant to output the String Doing that thing you do, but it doesn’t. [3 marks] What does it output? ANSWER: Doing that thing What simple fix (use the least amount of change to the existing code) will result in the correct output? ANSWER: replace the 3rd line with s = s.concat( " you do" ) public static void main ( String[] oneders ) { String s = "Doing that thing"; s.concat( " you do" ); /* line 3 */ System.out.println( s ); } 3. Consider the recursive function shown below. [5 marks] int f ( int n ) { if (n == 0) return 0; return n + f(n-1); } (a) [1 mark] What is f(3)? ANSWER: 6 (b) [2 marks] In general, what value is returned by f(n)? ANSWER: 1 + 2 + · · · + n = n(n + 1)/2 Give the simplest expression possible in each case. (c) [2 marks] You wouldn’t want someone writing an aircraft navigational system to use this code. Why not; what can go wrong? ANSWER: Consider f(-1); it is a non-terminating recursion. CSC 115/160 (S01), 4 4. Draw the boxes and arrows diagram corresponding to the code given below: (Assume class Node { int data; Node link; }) [5 marks] Node p = new Node(); p.link = new Node(); p.link.link = new Node(); p.link.link.link = p.link; ANSWER: p 0 0 0 5. In the following code write the line number(s) of the line containing the indicated item [5 marks] (a) A constructor ANSWER: line 5 (b) A javadoc comment ANSWER: line 6 (c) A run-time error ANSWER: line 7, attempt to dereference a null pointer (d) An instance method ANSWER: line 4 (e) A class variable ANSWER: line 3 /* /* /* /* /* /* /* /* /* 1 2 3 4 5 6 7 8 9 */ class X { */ int info = 99; // set info to 99; */ static X p; */ X Y ( int y ) { return null; } */ X ( int y ) { int z = 10; } */ public static void main ( String[] info ) { /** What, no args? */ */ System.out.println( p.info ); */ } */ } 6. What is the for-loop equivalent of the following linked list traversal? I.e., rewrite this code so that it uses a for-loop and not a while-loop. Class Node defines as in question 4. [3 marks] Node p = listHead; while (p != null) { System.out.println( p.data ); p = p.link; } ANSWER: for (Node p=listHead; p!=null; p=p.link) System.out.println( p.data ); CSC 115/160 (S01), 5 7. Given the class TreeNode below, write a method join that takes two binary trees T1 and T2 as input and that returns a binary tree whose left and right subtrees are T1 and T2 . Also, the value at the root of the new binary tree should be the sum of the values at the root of the two subtrees. This was the method mentioned in class when we were discussing the implementation of Huffman’s algorithm. [5 marks] public class TreeNode { double data; TreeNode llink, rlink; TreeNode join ( TreeNode T1, TreeNode T2 ) { // your code goes here TreeNode root = new TreeNode(); root.data = T1.data + T2.data; root.llink = T1; root.rlink = T2; return root; } } 8. Write a recursive method same that takes as input (has parameters) two binary trees and returns true if they are isomorphic (have exactly the same structure), and false otherwise. [5 marks] For example, for the trees below same(p,q) is false, and same(p,r) is true. public class TreeNode { TreeNode llink, rlink; boolean same ( TreeNode T1, TreeNode T2 ) { // Your code goes here if (T1==null || T2==null) return(T1==null && T2==null); return( same( T1.llink, T2.llink ) && same( T1.rlink, T2.rlink ) ); } } CSC 115/160 (S01), 6 9. Give the preorder, inorder, and postorder traversals of the following binary search tree. Then show the tree that results when you insert the value 6. [4 marks] Preorder: ANSWER: 9, 8, 3, 1, 5, 12, 20, 15, 21 Postorder: ANSWER: 1, 5, 3, 8, 15, 21, 20, 12, 9 Inorder: ANSWER: 1, 3, 5, 8, 9, 12, 15, 20, 21 ANSWER: 9 8 12 3 1 20 5 15 21 6 10. Suppose that the preorder traversal of a binary search tree resulted in the sequence of values 6, 5, 4, 2, 1, 3, 9, 8, 7, 11, 10. Draw the original binary search tree. [4 marks] ANSWER: 6 5 9 4 2 1 8 7 3 11 10 CSC 115/160 (S01), 7 11. Given an array A whose values in positions 1..10 are 5, 7, 13, 15, 18, 6, 10, 18, 12, 2 what is the result of heapify when applied to that array to turn it into a (max) heap? Draw your answer in the complete binary tree shown below. [4 marks] ANSWER: 18 18 15 18 or 13 5 6 18 10 15 7 12 2 13 7 6 10 5 12 2 What is the resulting heap if the items are successively inserted in that same order 6, 5, 4, 2, 1, 3, 9, 8, 7, 11, 10 into an initially empty heap? Draw your answer in the complete binary tree shown below. [4 marks] ANSWER: 11 10 7 6 9 2 5 1 8 3 4 CSC 115/160 (S01), 8 12. Build a Huffman tree based on the frequencies of occurance of the letters A-F given below. Draw your final tree neatly (no crossing edges) below, and arrange the leaves so that they are as close to being in alphabetical order as possible (i.e., A is the leftmost leaf, B as far to the left as possible after that, then C etc.) [4 marks] A B C D E F 24 26 5 7 14 24 ANSWER 00 10 1100 1101 111 01 100 1 0 48 0 24 A 52 1 24 F 0 26 B 1 26 1 0 12 0 5 C 1 14 E 7 D Encode the message BACCED, assuming that left branches are 0’s and right branches are 1’s. [2 marks] 10 00 1100 1100 111 1101 Decode the message 1001001101001101 (under the same assumption). [2 marks] BFADAD Write an expression (which you don’t have to simplify) that is equal to the expected number of bits needed to encode a character. [1 bonus mark] ANSWER: 1 (24 · 2 + 26 · 2 + 5 · 4 + 7 · 4 + 14 · 3 + 24 · 2) 100 CSC 115/160 (S01), 9 13. Below is shown the adjacency list representation of a (directed) graph. ANSWER: 0 1 2 3 4 5 4 4 1 5 3 5 3 4 1 0 5 2 5 (a) Draw the graph. [3 marks] (b) Finish the code below for doing a depth-first-traversal of a graph. [5 marks] (c) List the vertices (nodes) of the graph in the order that they are encountered in a depth-first-search traversal (i.e., in the order that they are printed by the println method call). [2 marks] ANSWER: 0, 5, 4, 3, 1, 2 (d) Given graph with n vertices (nodes) and m edges, what exactly is the number of times that the test ( p != null ) is executed? [1 bonus mark] ANSWER: n + m times (since each edge is examined once and p is null once for each vertex). class Graph { Edge[] adj; boolean[] beenThere; void dfs ( int v ) { System.out.println( v ); beenThere[v] = true; Edge p = adj[v]; while ( p != null ) { // FILL IN THE BODY OF THIS LOOP if (!beenThere[v]) dfs( p.vert ); p = p.link; } } void traverse() { for ( int i=0; i<adj.length; ++i ) beenThere[i] = false; for ( int i=0; i<adj.length; ++i ) if (!beenThere[i]) dfs( i ); } } // FILL IN // FILL IN // FILL IN // FILL IN CSC 115/160 (S01), 10 14. Draw the expression tree corresponding to the expression (x + 10)/5 − 19 ∗ (y − z). Give the postfix equivalent of that expression. [5 marks] x 10 + 5 / 19 y z − + − / + x * 5 10 19 y z 15. A stack can sometimes be used to sort a sequence of numbers. For example to sort the sequence 1, 2, 3, 4 do push(1), pop, push(2), pop, push(3), pop, push(4), pop. To sort the sequence 4, 3, 2, 1 do push(4), push(3), push(2), push(1), pop, pop, pop, pop. Note that the pushes must occur in the order that the items originally appear in the sequence; the pops can go anywhere that they are valid (i.e., the stack is not empty). [4 marks] Give a sequence of pushes and pops that will sort the sequence 5, 2, 1, 4, 3. ANSWER: push(5), push(2), push(1), pop, pop, push(4), push(3), pop, pop, pop Give an example of a small sequence of numbers that cannot be sorted with a stack. ANSWER: 2, 3, 1 Lots of other correct answers but there must be a sub-pattern of relative values of 2, 3, 1.