Key to CSE143 Midterm, Spring 2024 handout #17 1. 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 2. 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)); } } 3. 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 4. 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; -----------------------+-----------------------+------------------------------- 5. 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; } 6. One possible solution appears below. public static void alternatingReverse(Stack<Integer> s) { if (s.size() % 2 != 0) { throw new IllegalArgumentException(); } Queue<Integer> q = new LinkedList<Integer>(); int oldSize = s.size(); while (!s.isEmpty()) { q.add(s.pop()); } 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()); } while (!q.isEmpty()) { s.push(q.remove()); } }
Stuart Reges
Last modified: Fri May 10 14:10:01 PDT 2024