Corrections

Question 1

public static int numDivisors(int inputVal)
{
    int count = 0;
    for (int k =1; k<= inputVal; k++)
    {
        if (/*condition*/)
        {
            count++;
        }
    }
    return count;
}

I put C when the correct answer was A because if the remainder when you divide inputVal by k is 0, then inputVal is evenly divisible by k. We use the modulus operator (%) to determine the remainder when you divide inputVal by k.

Question 11

public static boolean mystery(String str)
{
    Srting temp = "";

    for (int k = str.length(); k>0; k--)
    {
        temp = temp + str.substring(k-1, k);
    }

    return temp.equals(str);
}

I put D when the correct answer was E because this algorithm assigns temp the letters of str in reverse by extracting each letter of str starting at the end and moving to the beginning. Each letter is appended to the end of temp. So, if str was “abc”, “c” would be appended followed by “b”, and then “a”, making temp equal to “cba”. This method will return true if str is equal to temp, which is a string with the letters of str in reverse. The string “noon” is the only string that is the same in reverse.

Question 39

List<String> students = new ArrayList<String>();

students.add("Alex");
students.add("Bob");
students.add("Carl");

for (int k = 0; k < students.size(); k++)
{
    System.out.print(students.set(k, "Alex") + " ");
}

System.out.println();

for (String str : students)
{
    System.out.print(str + " ");
}

I put D when the correct answer was C because the first for loop uses the set method to change the value of each element in students to “Alex”. When the set method is called, it returns the value that was originally at this index. So, the first loop will print Alex Bob Carl. At this point all elements have been set to “Alex”. The second for loop uses an enhanced for loop to access every element and will print Alex Alex Alex.