Key to CSE143X Sample Midterm, Autumn 2024    handout #10
1.      Expression                              Value
        -----------------------------------------------
        3 * (5 - 2) - 3 - 2 * 2                2
        4 * 7 % 8 + 132 % 10 + 3 % 4           9
        27 / 5 / 2 + 3.4 * 2 - 1.1 * 2         6.6
        9 + 9 + "9 + 9" + 9 + 9                "189 + 999"
        19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4   14.0
2. Parameter Mystery.  The program produces the following output.
    semi missing brace and a 42
    semi missing 42 and a 8
    brace missing literal and a semi
    84 missing 1 and a cse
3.      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
4. 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);
        }
    }
5.      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
6.      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;
-----------------------+-----------------------+-------------------------------
7. 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--;
        }
    }
8. One possible solution appears below.
    public void splitStack(Stack s) {
        Queue q = new LinkedList<>();
        stackToQueue(s, q);
        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);
            }
        }
        queueToStack(q, s);
        stackToQueue(s, q);
        queueToStack(q, s);
    }