26 min read

Question 1

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

int result = numDivisors(12);
System.out.println("The number of divisors for 12 is: " + result);
The number of divisors for 12 is: 6

Question 2

for (int r = 3; r > 0; r--)
{
  int c;
  for (c = 1; c < r; c++)
  {
    System.out.print("-");
  }
  for (c = r; c <= 3; c++)
  {
    System.out.print("*");
  }
  System.out.println();
}
--*
-**
***

Question 7

for (int outer = 1; outer <= 6; outer++)
{
    for (int inner = outer; inner <= 6; inner++)
    {
        if (inner % 2 == 0)
        {
            System.out.print(inner + " ");
        }
    }
    System.out.println();
}
2 4 6 
2 4 6 
4 6 
4 6 
6 
6 

Question 13

private int[] numbers;

public void mystery(int x) {
    for (int k = x; k < numbers.length; k = k + x) {
        numbers[k] = numbers[k - 1] + x;
    }
}

Question 15

System.out.println(404/10*10+1)
401

Question 22

Question 25

int count = 0;
for (int x = 0; x < 4; x++)
{
    for (int y = x; y < 4; y++)
    {
        count++;
    }
}
System.out.println(count)
10

Question 34

private List<String> listOfWords;
public String wordsWithCommas()
{
    String result = "(";
    int sizeofList = listOfWords.size();
    for (int k = 0; k < sizeOfList; k++)
    {
        result = result + listOfWords.get(k);
    }
    if (k != 0)
    {
        result = result + ", ";
    }
    result = result + "} ";
    return result;
}

Corrections

Question 4

image Explanation for Wrong Answer: B, Since the enhanced for loop checks all elements in arr, if the largest value was non-negative and occurred at arr[arr.length - 1], the algorithm would check this element and assign it to maxVal.

Explanation for Correct Answer: C Since maxVal is initialized to a value that is greater than all values in arr, maxVal will never be updated and 0 will be returned. To correct this, maxVal could be initialized to the first element in arr or initialized to Integer.MIN_VALUE.

Question 5

image

Explanation for Wrong Answer: D, The value of (x   y) could be true if x is false and y is true, however, the value of (x   y) && x would evaluate to false in this case. Therefore, these statements are not equivalent.
Explanation for Correct Answer: A, For the expression to evaluate to true, the expressions on either side of the && operator must be true. If x is true then x   y is true regardless of the value of y, meaning (x   y) && x evaluates to true. If x is false, the expression evaluates to false regardless of the value of (x   y).

Question 9

image

Explanation for Wrong Answer: A, This would result in an integer between 0 and 10 inclusive, since (int)(Math.random() *6) generates a single integer between 0 and 5 inclusive which is then multiplied by 2. Multiplying by 2 does not represent two independent random numbers being generated.

Explanation for Correct Answer: E, The call Math.random() will produce a double between 0 and 1, not including 1. To generate a random number in the range of 1 to 6, the call Math.random() needs to be multiplied by the number of integers you want to generate, in this case 6, giving us Math.random() * 6. This will result in a double between 0 and 6, not including 6. If we type cast this to an int, as in (int)(Math.random() * 6), the result will be an integer betwee n 0 and 5 inclusive. Adding 1 will adjust the range to 1 to 6 inclusive, as in (int)(Math.random() * 6) + 1. This expression will simulate the rolling of one number cube. Since each roll is independent, to simulate rolling two number cubes, we need to use this expression twice which simplifies to 2 + (int)(Math.random() * 6) + (int)(Math.random() * 6).

Question 13

image

Explanation for Wrong Answer: E, This would be the result if the code segment in the for loop was changed to numbers[k-1] = numbers[k-1] + 3.

Explanation for Correct Answer: A, The values of the loop control variable k starts at 1 and is incremented by 3 as long as k is less than numbers.length. As a result, k will have the values 1, 4, 7 and then when it becomes 10, the loop will end. Only the values in numbers at indexes 1, 4, and 7 will be updated to the value of the sum of the element at the index that is one less than k (the element before the one being updated) and 3. So, numbers[1] will be assigned the value of numbers[0] + 3 or 20, numbers[4] will be assigned the value of numbers[3] + 3 or 45, and numbers[7] will be assigned the value of numbers[6] + 3 or 51. All other values will be unchanged.

Question 16

image

Explanation for Wrong Answer: A, The largest value in the two-dimensional array is stored in found, but result is returned.

Explanation for Correct Answer: E, Prior to the start of the nested for loops, the value of found is initialized to the element at row 0, column 0 in values and result is initialized to 0.The outer for loop iterates across all rows in values. The inner for loop iterates across the column indices of each row. When an element is located in the two-dimensional array that is larger than the current value stored in found, then this value is assigned to found and the column index is assigned to result. When the nested loop structure stops executing, the largest element in values is stored in found and the column in which that element was located is stored in result.

Question 24

image

Explanation for Wrong Answer: B, Choice I will cause a compiler error if added to the SomeMethods class because it has the same method signature as public void one (int first), since it has the same name (one) and each parameter list consists of a single int parameter. Choice III can be added to the SomeMethods class because there are three int parameters and no other method named one has three int parameters.

Explanation for Correct Answer: D, A method signature consists of the method name and the ordered list of parameter types. Choice I will cause a compiler error if added to the SomeMethods class because it has the same method signature as public void one (int first), since it has the same name (one) and each parameter list consists of a single int parameter. Choice II can be added to the SomeMethods class because, although it has the same name (one), the parameter list has the types String, int which has a different order than the method public void one (int first, String second). Choice III can be added to the SomeMethods class because there are three int parameters and no other method named one has three int parameters.

Question 27

image

Explanation for Wrong Answer: D, This would be the result if we were finding the largest element instead of the smallest element and moving it into its proper sorted position.

Explanation for Correct Answer: B, The selection sort algorithm shown looks for the smallest value in the elements ranging from index j to the end and swaps the smallest value with the value at position j. In the first pass, 1 is the smallest element from position j = 0 to the end of the array, so it is swapped with 6.

Question 34

image

Explanation for Wrong Answer: A, List is an interface, which an ArrayList implements. Please note that List is no longer tested as part of the AP CSA exam and ArrayList will be used instead. The size of an ArrayList is found by calling the method size without subtracting 1. Subtracting 1 would cause the last element in the list to be skipped. We want to avoid adding a comma at the end of the list of words not after the first element, so the boolean condition should be k != sizeOfList – 1.

Explanation for Correct Answer: D, List is an interface, which an ArrayList implements. Please note that List is no longer tested as part of the AP CSA exam and ArrayList will be used instead. To determine the size of an ArrayList we need to call the method size(). Each word will be separated by a comma, but no comma should appear after the last element in the list. Therefore, a comma is added as long as k does not equal the last index, sizeOfList – 1, since list indices start at 0.

Reflection

Overall, the test was pretty hard with some easy and hard questions. I am happy with my score although I had a lot of help, but I should try to get a higher score. If I paid attention a little more, I think I could have understood the questions a bit more I think I went through this test pretty slowly because I did not really understand each question and I had to read it a lot of times.

I think I can do well on the AP Exam if I find out what exactly I need to do and keep reviewing the content, spend less time on each problem. I also need to take less time on the longer code functions because I spend a lot of time understanding how it works.