CSE143 Midterm Autumn 2017 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ Student #____________________________ 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 ArrayIntList 10 _____ 6 Stacks/Queues 25 _____ ---------------------------- Total 100 _____ This is a closed-book/closed-note exam. Space is provided for your 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 at the end of the exam 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 and smart watches. 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 a TA and exit quietly. 1. Recursive Tracing, 15 points. Consider the following method: public void mystery(int x, int y) { if (x > y) { System.out.print("*"); } else if (x == y) { System.out.print("=" + y + "="); } else { System.out.print(y + " "); mystery(x + 1, y - 1); System.out.print(" " + x); } } For each call below, indicate what output is produced: Method Call Output Produced mystery(3, 3); _____________________________________ mystery(5, 1); _____________________________________ mystery(1, 5); _____________________________________ mystery(2, 7); _____________________________________ mystery(1, 8); _____________________________________ 2. Recursive Programming, 15 points. Write a recursive method called undouble that takes a string as a parameter and that returns a new string obtained by replacing every pair of repeated adjacent letters with one of that letter. For example, the String "bookkeeper" has three repeated adjacent letters ("oo", "kk", and "ee"), so undouble("bookkeeper") should return the string "bokeper". Below are more sample calls: Method Value Method Value Call Returned Call Returned --------------------------------- --------------------------------- undouble("odegaard") "odegard" undouble("oops") "ops" undouble("baz") "baz" undouble("foobar") "fobar" undouble("mississippi") "misisipi" undouble("apple") "aple" undouble("carry") "cary" undouble("berry") "bery" undouble("juggle") "jugle" undouble("theses") "theses" undouble("little") "litle" undouble("") "" You may assume that the string is composed entirely of lowercase letters, as in the examples above, and that no letter appears more than two times in a row. Notice that the method might be passed an empty string, in which case it returns an empty string. You are not allowed to construct any structured objects to solve this problem other than strings (no 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. You may use only the string methods included on the cheat sheet. 3. Details of inheritance, 20 points. Assuming that the following classes have been defined: public class Green extends Red { public void method1() { System.out.println("Green 1"); } public void method3() { System.out.println("Green 3"); } } public class White extends Red { public void method2() { System.out.println("White 2"); } public void method3() { System.out.println("White 3"); } } public class Blue { public void method1() { System.out.println("Blue 1"); method2(); } public void method2() { System.out.println("Blue 2"); } } public class Red extends Blue { public void method2() { System.out.println("Red 2"); super.method2(); } } And assuming the following variables have been defined: Blue var1 = new Blue(); Green var2 = new Green(); Object var3 = new White(); Red var4 = new Green(); Blue var5 = new Red(); Blue var6 = new White(); 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.method1(); ____________________________ var2.method1(); ____________________________ var3.method1(); ____________________________ var4.method1(); ____________________________ var1.method2(); ____________________________ var2.method2(); ____________________________ var3.method2(); ____________________________ var4.method2(); ____________________________ var1.method3(); ____________________________ var2.method3(); ____________________________ var3.method3(); ____________________________ var4.method3(); ____________________________ ((Blue)var3).method1(); ____________________________ ((Red)var3).method2(); ____________________________ ((White)var3).method3(); ____________________________ ((White)var4).method3(); ____________________________ ((Green)var5).method3(); ____________________________ ((Red)var5).method1(); ____________________________ ((Blue)var6).method3(); ____________________________ ((Green)var6).method3(); ____________________________ 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] | p | | | | | q->[2]->[3] | q->[2]->[3]->[1] | | | -----------------------+-----------------------+------------------------------- | | | | p->[1]->[2]->[3] | p->[2]->[3] | | | | | q | q->[1] | | | -----------------------+-----------------------+------------------------------- | | | | p->[1]->[2] | p->[2] | | | | | q->[3]->[4] | q->[4]->[3]->[1] | | | | | | | | | -----------------------+-----------------------+------------------------------- | | | | p->[1]->[2] | p->[4]->[2]->[3] | | | | | q->[3]->[4]->[5] | q->[5]->[1] | | | | | | | | | | | | | | | | | -----------------------+-----------------------+------------------------------- 5. ArrayIntList, 10 points. Write a method called insertAt that takes an index, an integer n, and a value as parameters, and that inserts n copies of the given value at the given index in a list of integers. For example, if a variable called list stores this sequence: [12, 42, 8, 934] and the following call is made: list.insertAt(2, 4, 98); then it should store the following values after the call: [12, 42, 98, 98, 98, 98, 8, 934] ^ ^ +------------+ inserted values Notice that there are now four occurrences of the value 98 starting at index 2 and that later values are in the same order as in the original. 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> } Your method should throw an IllegalArgumentException if the index is less than 0 or greater than the number of elements in the list or if the value of n is less than 0. You are not to call any other ArrayIntList methods to solve this problem, you are not allowed to define auxiliary data structures (no array, ArrayList, etc), and your solution must run in O(n + m) time where n is the number of items to add and m is the length of the list. You may assume that the array has sufficient capacity to store the new values. 6. Stacks/Queues, 25 points. Write a method called alternatingReverse that takes a stack of integers as a parameter and that rearranges the values so that every other value starting from the bottom of the stack is reversed in order. For example, if a variable s stores these values: bottom [1, 2, 3, 4, 5, 6, 7, 8] top ^ ^ ^ ^ | | | | +-----+-----+-----+ sequence to reverse Starting from the bottom of the stack and looking at every other value, we find the sequence of numbers 1, 3, 5, 7. This sequence should be reversed while the other values should stay in the same positions. If we make the following call: alternatingReverse(s); the stack should store the following values after the call: bottom [7, 2, 5, 4, 3, 6, 1, 8] top ^ ^ ^ ^ | | | | +-----+-----+-----+ reversed sequence This example uses sequential integers to make it easier to see the sequence, but you should not assume anything about the sequence. For example, if s instead stored this sequence: bottom [7, 1, 4, 18, 23, 0, -5, 12] top then after the method is called, it would store this sequence: bottom [-5, 1, 23, 18, 4, 0, 7, 12] top Your method should throw an IllegalArgumentException if the number of elements in the stack is not an even number. 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). Space is provided on the next page for your answer. Please write your answer to alternatingReverse below.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011