Key to CSE143X Midterm, Autumn 2024 handout #17
1. Expression Value
-----------------------------------------------
8 * 2 - 2 * 3 10
87 % 10 + 28 % 5 % 2 8
1 + 2 + "3" + 4 + 5 * 6 "33430"
2 * 2.3 + 5 / 2 + 19 / 4 * 2.0 14.6
436 / 10 / 5 - 9 / 2 * 2.5 / 2 3.0
2. Parameter Mystery. The program produces the following output.
to walk the walk is good
to hear the good is bad
to feel the walk is song
to feel the talk is bad
3. Method Call Output Produced
------------------------------------------------
mystery(3, 3); =3=
mystery(5, 1); *
mystery(1, 5); 5 4 =3= 2 1
mystery(2, 7); 7 6 5 * 4 3 2
mystery(1, 8); 8 7 6 5 * 4 3 2 1
4. One possible solution appears below.
public static String undouble(String s) {
if (s.length() < 2) {
return s;
} else if (s.charAt(0) == s.charAt(1)) {
return s.charAt(0) + undouble(s.substring(2));
} else {
return s.charAt(0) + undouble(s.substring(1));
}
}
5. before after code
-----------------------+-----------------------+-------------------------------
p->[1] | p | q.next.next = p;
| | p = null;
q->[2]->[3] | q->[2]->[3]->[1] |
-----------------------+-----------------------+-------------------------------
p->[1]->[2]->[3] | p->[2]->[3] | q = p;
| | p = p.next;
q | q->[1] | q.next = null;
-----------------------+-----------------------+-------------------------------
p->[1]->[2] | p->[2] | q.next.next = q;
| | q = q.next;
q->[3]->[4] | q->[4]->[3]->[1] | q.next.next = p;
| | p = p.next;
| | q.next.next.next = null;
-----------------------+-----------------------+-------------------------------
p->[1]->[2] | p->[4]->[2]->[3] | q.next.next.next = p;
| | ListNode temp = q;
q->[3]->[4]->[5] | q->[5]->[1] | q = q.next.next;
| | temp.next.next = q.next.next;
| | p.next.next = temp;
| | p = temp.next;
| | p.next.next.next = null;
| | q.next.next = null;
-----------------------+-----------------------+-------------------------------
6. Statement Output
------------------------------------------------------------
var1.method1(); Blue 1/Blue 2
var2.method1(); Green 1
var3.method1(); compiler error
var4.method1(); Green 1
var1.method2(); Blue 2
var2.method2(); Red 2/Blue 2
var3.method2(); compiler error
var4.method2(); Red 2/Blue 2
var1.method3(); compiler error
var2.method3(); Green 3
var3.method3(); compiler error
var4.method3(); compiler error
((Blue)var3).method1(); Blue 1/White 2
((Red)var3).method2(); White 2
((White)var3).method3(); White 3
((White)var4).method3(); runtime error
((Green)var5).method3(); runtime error
((Red)var5).method1(); Blue 1/Red 2/Blue 2
((Blue)var6).method3(); compiler error
((Green)var6).method3(); runtime error
7. One possible solution appears below.
public void insertAt(int index, int n, int value) {
if (index < 0 || index > size || n < 0) {
throw new IllegalArgumentException();
}
for (int i = size - 1; i >= index; i--) {
elementData[i + n] = elementData[i];
}
for (int i = 0; i < n; i++) {
elementData[i + index] = value;
}
size += n;
}
8. One possible solution appears below.
public void alternatingReverse(Stack s) {
Queue q = new LinkedList();
int oldSize = s.size();
stackToQueue(s, q);
for (int i = 0; i < oldSize / 2; i++) {
s.push(q.remove());
q.add(q.remove());
}
while (!s.isEmpty()) {
q.add(q.remove());
q.add(s.pop());
}
queueToStack(q, s);
}