CSE143 Midterm Winter 2016 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ The exam is divided into six questions with the following points: # Problem Area Points Score --------------------------------------------- 1 Recursive Tracing 15 _____ 2 Recursive Programming 15 _____ 3 Details of Inheritance 20 _____ 4 Linked Lists 15 _____ 5 Stacks/Queues 25 _____ 6 Array Programming 10 _____ ---------------------------- Total 100 _____ This is a closed-book/closed-note exam. Space is provided for your answers. There is a "cheat sheet" at the end that you can use as scratch paper or to write answers. You can also request scratch paper from a TA. You are not allowed to access any of your own papers during the exam. The exam is not, in general, graded on style and you do not need to include comments. For the stack/queue question, however, you are expected to use generics properly and to declare variables using interfaces when possible. The cheat sheet mentions important restrictions on Stacks and Queues that you must follow. You are allowed to abbreviate "compiler error" and "runtime error" for the inheritance question (as in "ce" and "re" or "c.e." and "r.e."), but you should otherwise NOT use any abbreviations on the exam. You are NOT to use any electronic devices while taking the test, including calculators. Anyone caught using an electronic device will receive a 10 point penalty. Do not begin work on this exam until instructed to do so. Any student who starts early or who continues to work after time is called will receive a 10 point penalty. If you finish the exam early, please hand your exam to the instructor and exit quietly through the front door. 1. Recursive Tracing, 15 points. Consider the following method: public void mystery(int x) { if (x <= 0) { System.out.print("."); } else if (x % 2 == 0) { System.out.print(x % 10); mystery(x / 10); } else { mystery(x / 10); System.out.print(x % 10); } } For each call below, indicate what output is produced: Method Call Output Produced mystery(7); _____________________________________ mystery(246); ______________________________________ mystery(195); ______________________________________ mystery(1234); ______________________________________ mystery(29548); ______________________________________ 2. Recursive Programming, 15 points. Write a recursive method called printPattern that takes an integer n as a parameter and that prints exactly n characters composed of nested parentheses and square brackets. The nested parentheses and brackets should alternate with the innermost pair being parentheses. For example, printPattern(8) should print: [([()])] The output does not include a println. If n is odd, the method should print a period in the center, as in printPattern(9) which prints: [([(.)])] Below are more examples of calls and the output produced. Method Call Output Produced ------------------------------------- printPattern(1) . printPattern(2) () printPattern(3) (.) printPattern(4) [()] printPattern(5) [(.)] printPattern(6) ([()]) printPattern(7) ([(.)]) The method should produce no output when passed 0 and should throw an IllegalArgumentException if passed a negative value. You are not allowed to construct any structured objects to solve this problem (no string, array, ArrayList, StringBuilder, Scanner, etc) and you may not use a while loop, for loop or do/while loop to solve this problem; you must use recursion. 3. Details of inheritance, 20 points. Assuming that the following classes have been defined: public class Clock extends Bear { public void method3() { System.out.println("Clock 3"); } } public class Lamp extends Can { public void method1() { System.out.println("Lamp 1"); } public void method3() { System.out.println("Lamp 3"); } } public class Bear extends Can { public void method1() { System.out.println("Bear 1"); } public void method3() { System.out.println("Bear 3"); super.method3(); } } public class Can { public void method2() { System.out.println("Can 2"); method3(); } public void method3() { System.out.println("Can 3"); } } And assuming the following variables have been defined: Object var1 = new Bear(); Can var2 = new Can(); Can var3 = new Lamp(); Bear var4 = new Clock(); Object var5 = new Can(); Can var6 = new Clock(); In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a/b/c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with either the phrase "compiler error" or "runtime error" to indicate when the error would be detected. Statement Output ------------------------------------------------------------ var1.method2(); ____________________________ var2.method2(); ____________________________ var3.method2(); ____________________________ var4.method2(); ____________________________ var5.method2(); ____________________________ var6.method2(); ____________________________ var1.method3(); ____________________________ var2.method3(); ____________________________ var3.method3(); ____________________________ var4.method3(); ____________________________ var5.method3(); ____________________________ var6.method3(); ____________________________ ((Lamp)var6).method1(); ____________________________ ((Can)var1).method1(); ____________________________ ((Can)var1).method2(); ____________________________ ((Bear)var1).method3(); ____________________________ ((Bear)var6).method1(); ____________________________ ((Clock)var1).method1(); ____________________________ ((Clock)var4).method2(); ____________________________ ((Bear)var3).method1(); ____________________________ 4. Linked Lists, 15 points. Fill in the "code" column in the following table providing a solution that will turn the "before" picture into the "after" picture by modifying links between the nodes shown. You are not allowed to change any existing node's data field value and you are not allowed to construct any new nodes, but you are allowed to declare and use variables of type ListNode (often called "temp" variables). You are limited to at most two variables of type ListNode for each of the four subproblems below. You are writing code for the ListNode class discussed in lecture: public class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list <constructors> } As in the lecture examples, all lists are terminated by null and the variables p and q have the value null when they do not point to anything. before after code -----------------------+-----------------------+------------------------------- | | p->[1]->[2] | p->[1]->[2]->[3] | | | | | q->[3] | q | | | -----------------------+-----------------------+------------------------------- | | p->[1]->[2]->[3] | p->[1]->[3] | | | | | q | q->[2] | | | -----------------------+-----------------------+------------------------------- | | | | p->[1]->[2] | p->[2]->[3] | | | | | q->[3]->[4] | q->[1]->[4] | | | | | | | | | -----------------------+-----------------------+------------------------------- | | | | p->[1]->[2]->[3] | p->[3]->[1] | | | | | q->[4]->[5] | q->[5]->[2]->[4] | | | | | | | | | | | | | | | | | | | -----------------------+-----------------------+------------------------------- 5. Stacks/Queues, 25 points. Write a method called mirror that takes a stack of integers as a parameter and that doubles the size of the stack by appending the mirror image of the original sequence at the top of the stack. The mirror image is the same sequence of values in reverse order. For example, suppose that a variable called s stores the following values: bottom [1, 3, 2, 7] top and we make the following call: mirror(s); then it should store the following values after the call: bottom [1, 3, 2, 7, 7, 2, 3, 1] top Notice that it has been doubled in size by having the original sequence appear in reverse order at the top of the stack. If the original stack is empty, your method should not change it. You are to use one queue as auxiliary storage to solve this problem. You may not use any other auxiliary data structures to solve this problem, although you can have as many simple variables as you like. You also may not solve the problem recursively. Your solution must run in O(n) time where n is the size of the stack. Use the Stack and Queue structures described in the cheat sheet and obey the restrictions described there (recall that you can't use the peek method or a foreach loop or iterator). 6. Array Programming, 10 points. Write a method called removeRange that takes as parameters a "from" index (inclusive) and "to" index (exclusive) and that removes the values in the given range from a list of integers. For example, if a variable called list stores the following values: [8, 13, 17, 4, 9, 12, 98, 41, 7, 23, 0, 92] and you make the following call: list.removeRange(3, 8); values starting at index 3 (the value 4) and up to but not including index 8 (the value 7) are removed: [8, 13, 17, 4, 9, 12, 98, 41, 7, 23, 0, 92] | | +--------------+ to remove thus, the list should store the following values after the call: [8, 13, 17, 7, 23, 0, 92] Your method should throw an IllegalArgumentException if either index is not legal. Start has to be less than or equal to stop and both have to be between 0 and the size of the list inclusive. If start equals stop, the method should not change the list because the specified sublist is empty. You are writing a method for the ArrayIntList class discussed in lecture: public class ArrayIntList { private int[] elementData; // list of integers private int size; // current # of elements in the list <methods> } You may not call any other methods of the ArrayIntList class to solve this problem, you are not allowed to define any auxiliary data structures (no array, String, ArrayList, etc), and your solution must run in O(n) time. You are not allowed to call System.arraycopy to solve this problem. CSE143 Midterm Cheat Sheet Queues should be constructed using the Queue<E> interface and the LinkedList<E> implementation. For example, to construct a queue of String values, you would say: Queue<String> q = new LinkedList<String>(); Stacks should be constructed using the Stack<E> class (there is no interface). For example, to construct a stack of String values, you would say: Stack<String> s = new Stack<String>(); To transfer from a queue to a stack: while (!q.isEmpty()) { s.push(q.remove()); } To transfer from a stack to a queue: while (!s.isEmpty()) { q.add(s.pop()); } ------------------------------------------------------------------------------- For Stack<E>, you are limited to the following operations: public void push(E value); // push given value onto top of the stack public E pop(); // removes and returns the top of the stack public boolean isEmpty(); // returns whether or not stack is empty public int size(); // returns number of elements in the stack For Queue<E> you are allowed the following operations: public void add(E value); // inserts given value at the end of the queue public E remove(); // removes and returns the front of the queue public boolean isEmpty(); // returns whether or not queue is empty public int size(); // returns number of elements in the queue YOU ARE NOT ALLOWED TO USE FOREACH LOOPS OR ITERATORS FOR EITHER STRUCTURE. -------------------------------------------------------------------------------
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011