CSE143 Midterm
Winter 2014
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) {
if (x < 10) {
System.out.print(x);
} else {
int y = x % 10;
System.out.print(y);
mystery(x / 10);
System.out.print(y);
}
}
For each call below, indicate what output is produced:
Method Call Output Produced
mystery(7); _______________________
mystery(38); _______________________
mystery(194); _______________________
mystery(782); _______________________
mystery(3842); _______________________
2. Recursive Programming, 15 points. Write a recursive method called showSplit
that takes an integer n as a parameter and that prints a detailed
description of how n would be split in half by an algorithm like merge sort.
Algorithms like merge sort divide an input into two halves of approximately
equal size. These two halves are then divided again and the dividing
continues until you get to an input of size 1.
When n is less than 2, there is no splitting necessary, so it simply prints
the value. Thus, the following calls:
showSplit(1);
System.out.println(); // to complete the line of output
showSplit(0);
System.out.println(); // to complete the line of output
would produce these two lines of output:
1
0
When n is at least 2, the method should print the value followed by an
equals sign and then in parentheses show the two halves separated by a
comma. Thus, these calls:
showSplit(2);
System.out.println(); // to complete the line of output
showSplit(4);
System.out.println(); // to complete the line of output
would produce the following two lines of output:
2 = (1, 1)
4 = (2 = (1, 1), 2 = (1, 1))
(continued on next page)
Notice that in splitting 4, it indicates that it is split into two
occurrences of 2, but each of these values needs to be split further and
that detail is included in the output. This output can become quite long.
For example, the call showSplit(8) would produce the following output:
8 = (4 = (2 = (1, 1), 2 = (1, 1)), 4 = (2 = (1, 1), 2 = (1, 1)))
Odd values greater than 2 do not split evenly. In these cases, your method
should include the extra value in the first part of the split. The calls:
showSplit(3);
System.out.println(); // to complete the line of output
showSplit(7);
System.out.println(); // to complete the line of output
would produce the following output:
3 = (2 = (1, 1), 1)
7 = (4 = (2 = (1, 1), 2 = (1, 1)), 3 = (2 = (1, 1), 1))
Your method should throw an IllegalArgumentException if the value passed as
a parameter is less than 0. 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. 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] | p->[2] |
| |
| |
q->[3] | q->[3]->[1] |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1] | p->[3] |
| |
| |
q->[2]->[3] | q->[1]->[2] |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1]->[2] | p->[3]->[2]->[1] |
| |
| |
q->[3]->[4] | q->[4] |
| |
| |
| |
| |
-----------------------+-----------------------+-------------------------------
| |
| |
p->[1]->[2] | p->[1]->[3]->[5] |
| |
| |
q->[3]->[4]->[5] | q->[2]->[4] |
| |
| |
| |
| |
| |
| |
| |
-----------------------+-----------------------+-------------------------------
4. Details of inheritance, 20 points. Assuming that the following classes have
been defined:
public class Neck extends Head {
public void method2() {
System.out.println("Neck 2");
}
}
public class Head extends Arm {
public void method1() {
System.out.println("Head 1");
super.method2();
}
public void method2() {
System.out.println("Head 2");
}
}
public class Leg {
public void method2() {
System.out.println("Leg 2");
}
}
public class Arm extends Leg {
public void method3() {
System.out.println("Arm 3");
method2();
}
}
And assuming the following variables have been defined:
Arm var1 = new Neck();
Leg var2 = new Leg();
Leg var3 = new Arm();
Object var4 = new Head();
Arm var5 = new Head();
Head var6 = new Neck();
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(); ____________________________
((Neck)var5).method1(); ____________________________
((Arm)var3).method3(); ____________________________
((Arm)var4).method1(); ____________________________
((Neck)var1).method1(); ____________________________
((Head)var4).method1(); ____________________________
((Arm)var2).method3(); ____________________________
((Head)var3).method1(); ____________________________
((Leg)var4).method2(); ____________________________
5. Stacks/Queues, 25 points. Write a method called rearrangeDuplicates that
takes a sorted queue of integers as a parameter and that moves any duplicate
values to the end of the queue preserving the relative order of the values.
You should assume that the queue you are passed as a parameter has been
sorted in nondecreasing order. As a result, any duplicates will be grouped
together. Suppose, for example, that a variable q stores these values:
front [-10, -5, -5, -5, 3, 3, 3, 18, 19, 23, 23, 23, 23, 405] back
This sequence has duplicates of -5, 3, and 23. If we make the call:
rearrangeDuplicates(q);
then the duplicates are moved to the end of the queue:
front [-10, -5, 3, 18, 19, 23, 405, -5, -5, 3, 3, 23, 23, 23] back
^ ^ ^ ^
| | | |
+-----unique values-----+ +-----duplicates-----+
Notice that the new sequence contains the unique values from the original
sequence followed by the duplicates. Also notice that the two sublists are
still in their original order.
You are to use one stack 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 queue. 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 removeLast that takes
an integer n as a parameter and that removes the last occurrence of n from a
list of integers if it exists. For example, if a variable called list
stores this sequence of values:
[3, 5, 7, 3, 19, 42, 8, 23, 5, 5, 5, 7, 2, -8, 3, 5, -3]
and the following call is made:
list.removeLast(3);
Then the final occurrence of 3 is removed, leaving this list:
[3, 5, 7, 3, 19, 42, 8, 23, 5, 5, 5, 7, 2, -8, 5, -3]
If the same call is made again, then the list becomes:
[3, 5, 7, 19, 42, 8, 23, 5, 5, 5, 7, 2, -8, 5, -3]
Notice that in each case the last occurrence of the value is removed. If
the value does not appear in the list, then the method should leave 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 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.
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