CSE143 Midterm
Spring 2013
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 Linked Lists 15 _____
4 Details of Inheritance 20 _____
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 are not allowed to access any other paper during the exam
(not even scratch paper). Use the backs of the pages of this test if
necessary. Anyone caught with extra paper will lose at least 10 points.
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, int y) {
if (y == 1)
System.out.print(x);
else {
System.out.print(x * y + ", ");
mystery(x, y - 1);
System.out.print(", " + x * y);
}
}
For each call below, indicate what output is produced:
Method Call Output Produced
mystery(4, 1); _______________________
mystery(4, 2); _______________________
mystery(8, 2); _______________________
mystery(4, 3); _______________________
mystery(3, 4); _______________________
2. Recursive Programming, 15 points. Write a recursive method called
sameDashes that takes two equal length strings as parameters and that
returns whether or not they have dashes in the same positions (returning
true if they do and returning false otherwise). For example, below are four
pairs of strings that have the same pattern of dashes.
string1: "hi--there-you." "-15-389" "criminal-plan" "abc"
|| | | | |
string2: "12--(134)-7539" "-xy-zzy" "(206)555-1384" "9.8"
Below are examples where the dashes don't match, sometimes because the
number of dashes is different and sometimes because the positions of the
dashes are different:
string1: "is-this-okay?" "1-2-3-4" "a--b--c--d" "384.5"
string2: "78--3421789!!" "9876543" "---1234---" "718-3"
Your method should throw an IllegalArgumentException if the two strings are
not of the same length. In implementing this method, you are restricted to
the following string methods:
charAt(index) returns the character at the given index
equals(other) returns true if this string equals the other
length() returns the length of the string
substring(start, stop) returns a new string composed of characters
from start index (inclusive) to stop index
(exclusive)
substring(start) returns a new string composed of characters
from start index (inclusive) to the end of the
string
You are not allowed to construct any structured objects 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.
3. 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 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
}
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]->[3] | p->[1]->[3] |
| |
| |
q | q->[2] |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1]->[3] | p->[1]->[2]->[3] |
| |
| |
q->[2] | q |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1]->[2] | p->[2]->[4] |
| |
| |
q->[3]->[4] | q->[3]->[1] |
| |
| |
| |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1]->[2]->[3] | p->[3]->[1]->[5] |
| |
| |
q->[4]->[5] | q->[4]->[2] |
| |
| |
| |
| |
| |
| |
| |
-----------------------+-----------------------+-------------------------------
4. Details of inheritance, 20 points. Assuming that the following classes have
been defined:
public class Wall extends Dragon {
public void method1() {
System.out.println("Wall 1");
}
public void method3() {
System.out.println("Wall 3");
}
}
public class Raven {
public void method1() {
System.out.println("Raven 1");
}
public void method2() {
System.out.println("Raven 2");
method1();
}
}
public class Dragon extends Raven {
public void method1() {
System.out.println("Dragon 1");
super.method1();
}
}
public class Hand extends Raven {
public void method3() {
System.out.println("Hand 3");
}
}
And assuming the following variables have been defined:
Raven var1 = new Wall();
Raven var2 = new Dragon();
Raven var3 = new Raven();
Object var4 = new Dragon();
Dragon var5 = new Wall();
Object var6 = new Hand();
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(); ____________________________
var5.method1(); ____________________________
var6.method1(); ____________________________
var1.method2(); ____________________________
var2.method2(); ____________________________
var3.method2(); ____________________________
var4.method2(); ____________________________
var5.method2(); ____________________________
var6.method2(); ____________________________
((Dragon)var1).method3(); ____________________________
((Hand)var6).method3(); ____________________________
((Wall)var4).method1(); ____________________________
((Raven)var6).method2(); ____________________________
((Raven)var4).method1(); ____________________________
((Wall)var6).method3(); ____________________________
((Wall)var3).method3(); ____________________________
((Wall)var5).method3(); ____________________________
5. Stacks/Queues, 25 points. Write a method called isSorted that takes a stack
of integers as a parameter and that returns whether or not the values in the
stack appear in sorted (nondecreasing) order starting from the bottom of the
stack (returning true if it does, returning false otherwise). For example,
if a stack s stores the following values:
bottom [-5, 0, 3, 18, 23, 42, 208] top
then the call isSorted(s) should return true. If the stack had instead
contained this set of values:
bottom [-5, 0, 3, 18, 23, 42, 208, 17, 20, 94] top
then the call should return false because 208 is followed by numbers smaller
than it (17, 20, and 94).
Your method must restore the stack so that it stores the same sequence of
values after the call as it did before. Any stack with fewer than two
values should be considered to be sorted.
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 collapse that collapses
a list of integers by replacing each successive pair of integers with the
sum of the pair. For example, if a variable called list stores this
sequence of values:
[7, 2, 8, 9, 4, 13, 7, 1, 9, 10]
and the following call is made:
list.collapse();
The first pair should be collapsed into 9 (7 + 2), the second pair should be
collapsed into 17 (8 + 9), the third pair should be collapsed into 17 (4 +
13) and so on to yield:
[9, 17, 17, 8, 19]
If the list stores an odd number of elements, the final element is not
collapsed. For example, the sequence:
[1, 2, 3, 4, 5]
would collapse into:
[3, 7, 5]
with the 5 at the end of the list unchanged.
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
}
You may not call any other methods of the ArrayIntList methods 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.
CSE143 Midterm Cheat Sheet
Queues should be constructed using the Queue interface and the
LinkedList implementation. For example, to construct a queue of String
values, you would say:
Queue q = new LinkedList();
Stacks should be constructed using the Stack class (there is no interface).
For example, to construct a stack of String values, you would say:
Stack s = new Stack();
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, 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 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