Key to CSE143 Sample Midterm, Autumn 2023 handout #13 1. Method Call Output Produced ------------------------------------------------ mystery(9) **=9 mystery(42) *2*4*= mystery(703) *3*0**=7 mystery(5821) *1*2***=58 mystery(83105) **0*1*3**=85 2. One possible solution appears below. public int countDigit(int digit, int number) { if (number < 0 || digit < 0 || digit >= 10) { throw new IllegalArgumentException(); } if (number < 10) { if (number == digit) { return 1; } else { return 0; } } else if (number % 10 == digit) { return 1 + countDigit(digit, number / 10); } else { return countDigit(digit, number / 10); } } 3. Statement Output ------------------------------------------------------------ var1.method1(); Wall 1 var2.method1(); Dragon 1/Raven 1 var3.method1(); Raven 1 var4.method1(); compiler error var5.method1(); Wall 1 var6.method1(); compiler error var1.method2(); Raven 2/Wall 1 var2.method2(); Raven 2/Dragon 1/Raven 1 var3.method2(); Raven 2/Raven 1 var4.method2(); compiler error var5.method2(); Raven 2/Wall 1 var6.method2(); compiler error ((Dragon)var1).method3(); compiler error ((Hand)var6).method3(); Hand 3 ((Wall)var4).method1(); runtime error ((Raven)var6).method2(); Raven 2/Raven 1 ((Raven)var4).method1(); Dragon 1/Raven 1 ((Wall)var6).method3(); runtime error ((Wall)var3).method3(); runtime error ((Wall)var5).method3(); Wall 3 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 removeLast(int n) { int index = -1; for (int i = 0; i < size; i++) { if (elementData[i] == n) { index = i; } } if (index != -1) { for (int j = index; j < size - 1; j++) { elementData[j] = elementData[j + 1]; } size--; } } 6. One possible solution appears below. public void splitStack(Stack<Integer> s) { Queue<Integer> q = new LinkedList<>(); while (!s.isEmpty()) { q.add(s.pop()); } int oldSize = q.size(); for (int i = 0; i < oldSize; i++) { int n = q.remove(); if (n % 2 == 0) { q.add(n); } else { s.push(n); } } while (!q.isEmpty()) { s.push(q.remove()); } while (!s.isEmpty()) { q.add(s.pop()); } while (!q.isEmpty()) { s.push(q.remove()); } }
Stuart Reges
Last modified: Fri Oct 27 17:11:51 PDT 2023