See the criteria on Gradescope for how grades of E/S/N were assigned for each problem.

Question 1 - Conceptual

Part A: Expressions

Expression Result
(1 - 8 % 4) + 7 / 9 1
2 * 1 + (7 / 2.0) == 4 % 3 - 6.5 false
35 <= 36 % 40 || 0 != 10 % 5 true

Part B Conditional Tracing

  • Option B: m(5, 0);
  • Option E: m(10, -10);

Part C Explain in Plain English

Option C “Counts the number of even and odd values and returns an array of indices for all odd value elements, with all extra indices set to -1”

(Option E was the next closest option (though still incorrect), so students who selected E received an S grade for this part.)

Question 2 - Array Code Tracing

Part A: Contents of arr: [-15, 1, 2, 1, -15]

Part B: Contents of result: [9, 5, 2, 5, 9]

Part C: Describe the method

Option F: “Returns a new array containing the first half of the values in list mirrored across the array, and modifies list to contain a mirror image of its latter half instead of its first half.”

Question 3 - Debugging

  • Bug 1: The while loop condition should be changed from curr <= end to curr < end.
  • Bug 2: The random number generation should use the formula rand.nextInt(high - low + 1) + low (the existing code didn’t include the + 1).

Question 4 - General Programming

One possible solution appears below.

public static double garageSale(Scanner console, double money) {
    double mostExpensive = 0;
    int totalCount = 0;
    while (money >= 5) {
        System.out.print("Price? ");
        double price = console.nextDouble();
        System.out.print("Quantity? ");
        int quantity = console.nextInt();
        double nextPrice = quantity * price;
        if (nextPrice <= money && price < 10) {
            System.out.println("What a deal! I’ll buy it.");
            money = money - nextPrice;
            totalCount = totalCount + quantity;
            if (nextPrice > mostExpensive) {
                mostExpensive = nextPrice;
            }
        }
        System.out.println("Remaining money: $" + money);
        System.out.println();
    }
    System.out.println("Total quantities purchased: " + totalCount);
    System.out.println("Most expensive purchase: $" + mostExpensive);
    return money;
} 

Question 5 - File I/O Programming

One possible solution appears below.

public static void fixIndentation(Scanner input) {
    int indent = 0;
    while(input.hasNextLine()) {
        String line = input.nextLine();
        if (line.startsWith("}")) {
            indent--;
        }
        for (int i = 0; i < indent; i++) {
            System.out.print("\t");
        }
        if (line.endsWith("{")) {
            indent++;
        }
        System.out.println(line);
    }
}

Question 6 - Arrays Programming

Three possible solutions appear below.

public static boolean isReverse(int[] a, int[] b) {
    if (a.length != b.length) {
        return false;
    }

    int lastIndex = b.length - 1;

    for (int i = 0; i < a.length; i++) {
        if (a[i] != b[lastIndex - i]) {
            return false;
        }
    }   
    return true;
}

public static boolean isReverse(int[] a, int[] b) {
    if (a.length != b.length) {
        return false;
    }

    int lastIndex = b.length - 1;
    boolean flag = true

    for (int i = 0; i < a.length; i++) {
        if (a[i] != b[lastIndex - i]) {
            flag = false;
        }
    }

    return flag;
}

public static boolean isReverse(int[] a, int[] b) {
    if (a.length != b.length) {
        return false;
    }

    int lastIndex = b.length - 1;
    int diff = 0;
    for (int i = 0; i < a.length; i++) {
        if (a[i] != b[lastIndex - i]) {
            diff++;
        }
    }
    if (diff != 0) {
        return false;
    } else {
        return true;
    }
}