Key to CSE143X Midterm, Autumn 2025 handout #17 1. Method Call Output Produced ------------------------------------------------ mystery(7); 7 mystery(38); 3, 2 mystery(749); 7, 1, 0 mystery(-6842); 6, 2, 1, 2 mystery(954023); 9, 2, 1, 0, 2, 0 2. One possible solution appears below. public int weave(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException(); } if (x == 0 && y == 0) { return 0; } else { return 100 * weave(x / 10, y / 10) + 10 * (x % 10) + y % 10; } } 3. Statement Output ------------------------------------------------------------ var1.method2(); compiler error var2.method2(); Bunch 2/Bunch 3 var3.method2(); Bunch 2/Bit 3 var4.method2(); Bunch 2/Page 3 var5.method2(); compiler error var6.method2(); Bunch 2/Page 3 var1.method3(); compiler error var2.method3(); Bunch 3 var3.method3(); Bit 3 var4.method3(); Page 3 var5.method3(); compiler error var6.method3(); Page 3 ((Bit)var6).method1(); runtime error ((Bunch)var1).method1(); compiler error ((Bunch)var1).method2(); Bunch 2/Folder 3/Bunch 3 ((Folder)var1).method3(); Folder 3/Bunch 3 ((Folder)var6).method1(); Folder 1 ((Page)var1).method1(); runtime error ((Page)var4).method2(); Bunch 2/Page 3 ((Folder)var3).method1(); runtime error 4. before after code -----------------------+-----------------------+------------------------------- p->[1]->[2]->[3] | p->[1]->[2] | q = p.next.next; | | p.next.next = null; q | q->[3] | -----------------------+-----------------------+------------------------------- p->[1]->[3] | p->[1]->[2]->[3] | q.next = p.next; | | p.next = q; q->[2] | q | q = null; -----------------------+-----------------------+------------------------------- p->[1]->[2] | p->[2]->[4] | p.next.next = q.next; | | q.next = p; q->[3]->[4] | q->[3]->[1] | p = p.next; | | q.next.next = null; -----------------------+-----------------------+------------------------------- p->[1]->[2]->[3] | p->[2]->[1]->[4] | p.next.next.next = q.next; | | ListNode temp = q; q->[4]->[5] | q->[3]->[5] | q = p.next.next; | | p.next.next = p; | | p = p.next; | | p.next.next = temp; | | temp.next = null; -----------------------+-----------------------+------------------------------- 5. One possible solution appears below. public ArrayIntList reverseCopy() { ArrayIntList result = new ArrayIntList(elementData.length); for (int i = 0; i < size; i++) { result.elementData[i] = elementData[size - 1 - i]; } result.size = size; return result; } 6. One possible solution appears below. public void mirror(Stack<Integer> s) { Queue<Integer> q = new LinkedList<Integer>(); while (!s.isEmpty()) { q.add(s.pop()); } while (!q.isEmpty()) { s.push(q.remove()); } while (!s.isEmpty()) { q.add(s.pop()); } for (int i = 0; i < q.size(); i++) { int n = q.remove(); s.push(n); q.add(n); } while (!s.isEmpty()) { q.add(s.pop()); } while (!q.isEmpty()) { s.push(q.remove()); } }
Stuart Reges
Last modified: Fri Nov 14 12:36:39 PST 2025