2004 AP Computer Science A Exam Multiple Choice Explanations

The College Board released the complete 2004 AP Computer Science A Exam, including the multiple choice section. While their released exam includes detailed explanations for each free response question, it does not offer detailed explanations for each multiple choice question. Question by question explanations for the 2004 AP Computer Science A Multiple Choice Section are below.

You must purchase the released exam itself from the College Board’s Online Store. Refer to the College Board product for the original questions and for the letter answers.

Review the 2004 AP CS A Exam Multiple Choice with AP CS Tutor Brandon Horn.

Recommended Practice Topics

Question 1

Case I incorrectly evaluates to true if and only if all 3 of the conditions are true.

Case II correctly evaluates to true if and only if at least 1 of the conditions is true.

Case III computes the product of each difference. If at least 2 of the numbers are equal, at least 1 of the differences will be 0. If at least 1 of the differences is 0, the product will be 0 and the condition will evaluate to true. This correctly determines whether at least 2 of the value are equal.

Question 2

In a sorted array (Case II), inserting a word requires determining the position to insert and shifting elements to make space. In an unsorted array (Case I), a word can be inserted at the end without any comparisons or shift. Answer A states the opposite.

In an unsorted array, finding a word requires a sequential search through each word. In a sorted array, finding a word can be accomplished with a must faster binary search. Answer C states this correctly while Answer B states the opposite.

Since the list is sorted in alphabetical order, rather than by word length, finding the longest word requires visiting each word regardless of the order in which the words are stored.

In an unsorted array, finding the first word in alphabetical order requires visiting each word. In a sorted array, the first word is the first word in alphabetical order. Answer E states the opposite.

Question 3

The advantage of an ArrayList over an array is that an ArrayList resizes itself automatically. The length of an array must be specified at construction and cannot be changed.

An ArrayList does not offer a speed advantage for access, nor does it use less memory as Answers A and B state.

An ArrayList can only store objects; however, an array can store either objects or primitive types. This is an advantage of an array, not of an ArrayList.

Accessing the number of items in an ArrayList is accomplished as list.size(). Accessing the number of items in a list is accomplished as array.length.

Question 4

Answer A incorrectly switches <= and >=.

Answer B has the same problem as Answer A and also incorrectly uses || instead of &&.

Answer C is not syntactically correct. Two separate boolean conditions are required.

Answer D correctly checks that x is at least lower and that it is no larger than upper.

Answer E incorrectly checks if either condition is true rather than if both are true.

Question 5

Case I is syntactically incorrect because it attempts to access the private instance field y.

Case II correctly runs the setY method from the EnhancedData class.

Case III correctly runs the setX method from the Data class, which is the superclass of EnhancedData.

Question 6

Both s1 and s2 start at 0 and are incremented by num each time num meets certain conditions.

s1 is incremented if num is positive and is a multiple of 2. At the end of the method, s1 is the sum of all positive even values in the array.

Question 7

In order for the else statement to run, one or both of the conditions in the if statement that increments s1 must be false. The first condition will be false when num is negative or 0. The second condition will be false when num is not a multiple of 2.

The if statement after the else will be true when num is negative. Since the else statement will be run for all negative values of num (as well as for some positive values), s2 is the sum of all negative values in the array.

Question 8

Answer A is incorrect because using inheritance results in more classes, not fewer.

Answer B is correct because subclasses can call superclass methods using the keyword super. This is a substantial benefit of inheritance.

Answer C is incorrect because all classes allow for data encapsulation, not just inherited classes. (Data encapsulating refers to the practice of exposing only behavior, in the form of methods, publicly while keeping all data, in the form of instance fields. private.)

Answer D does not make sense.

Answer E is incorrect because applications with more classes (see the explanation for Answer A) typically compile more slowly.

Question 9

When conditionalTest is called, a is assigned the value 3 and b is assigned the value -2.

The first if statement evaluates to false because b is not greater than 0. Execution moves to the else if which evaluates to true because b is less than 0. The method prints C.

Due to short circuit or lazy evaluation the condition after the || is never evaluated.

Question 10

Case I correctly runs the getValue method to obtain the stored value as an int, which it then correctly prints.

Case II incorrectly attempts to access the private instance field myStoredValue.

Case III prints m which automatically runs the toString method of IntCell. The toString method of IntCell returns the stored value as a string, which Case III correctly prints.

Question 11

After the first assignment, x stores twice the value it stored at the start of the method. If the first and second statements were combined, they would be equivalent to x = 2x + 2x. If all 3 assignment statements were combined, they would be equivalent to x = 4x + 4x or 8 * x.

Question 12

The loop executes for each value of k from 1 to 19, inclusive. The if statement is true if the remainder when k is divided by 3 is 1. This is true for the values below, which are the values printed.

1 4 7 10 13 16 19

Question 13

The line prod = prod * k is immediately after the assignment the user’s input to k. If the user enters a negative number, it is included in the product.

The loop will still terminate when the user enters a negative number; however, the number will also be incorrectly included in the product.

Question 14

valueOne and valueTwo are both of the primitive type int. Primitive types can be compared using ==. Methods cannot be run on primitive types. Each answers except Answer B incorrectly runs a method on valueOne.

Question 15

Each return statement is inside an if statement. This means that there might be a path through the code that does not result in someCode returning an int.

To correct the issue, someCode must have a return statement within an else or entirely outside of an if statement.

It does not matter whether it is actually possible for all of the conditions to evaluate to false. The Java compiler does not reason about the contents of the conditions. For example, the method below has the same error as someCode.

public int someMethod(int a)
  if(a >= 0)
    return 0;

  if(a < 0)
    return 1;

Question 16

The values of num1 and num2 after each run of the loop are below.

Before 1st run: 0 3
After 1st run: 2 2
After 2nd run: 4 1
After 3rd run: 6 0

The loop terminates because num2 is 0. The second condition is never evaluated, so no exception is generated.

Question 17

For an inheritance hierarchy to make sense, any instance of the subclass must also be an instance of the superclass.

In Answer A, Animal incorrectly extends Bear. Not all animals are bears, so the hierarchy doesn’t make sense.

In Answer B, Bear correctly extends Animal. All bears are animals, so the hierarchy makes sense. A zoo stores animals, and an Animal[] is an acceptable way to store them.

In Answer C, Animal incorrectly extends Zoo. Also, Animal incorrectly stores a Bear as an instance fields.

Answer D is both syntactically and logically incorrect. A class can have at most 1 superclass. Also, a bear is not a zoo.

In Answer E, Bear incorrectly implements Zoo. The problem description states that a zoo contains many animals, not that is specifies methods that a bear must implement.

Question 18

selectSort attempts to implement the selection sort algorithm. pos is intended to store the position of the smallest element from j to numbers.length – 1, inclusive.

The implementation incorrectly initializes pos to 0, the first element in the array. This works only for the first run of the outer loop. On the second run, pos will incorrectly remain 0 since the smallest element has previously been swapped to the beginning of the array.

To correct the issue, pos should be initialized to j on Line 2.

Question 19

When n < 0, f1 explicitly returns 0 and f2 returns 0 because the loop never runs.

When n is 0, f1 calls f(-1) and returns the sum of the result and 0, or 0. f2 returns 0 because the loop still never runs.

Case III is more interesting. In both f1 and f2, the value of n is decremented by 1 for each iteration or recursive call. The methods will always return the same value. For each run, the value n * 10 is added to the result. For example, consider f1(3) and f2(3).

= f1(2) + 30
= f1(1) + 20 + 30
= f1(0) + 10 + 20 + 30
= 0 + 10 + 20 + 30

In f2(3), the same result is accomplished iteratively. For each value of n, the value of answer is shown below.

3: answer = 0 + 30
2: answer = 0 + 30 + 20
1: answer = 0 + 30 + 20 + 10

Question 20

Statement I causes a compile time error because methodB is not in class A and class A does not know about class B B.

Statement II does not cause an error because methodA is in class A and class B extends class A and inherits all public methods.

Statement III causes a compile time error because a1 is private in class A. A subclass cannot access private instance fields of its superclass.

Questions 21-25

These questions referenced the old Marine Biology Case Study and are not relevant to the new GridWorld Case Study.

Question 26

My Stack based trace of 2004 AP CS A Multiple Choice #26 shows a step by step solution.

Some of my students prefer to solve this using a different method, shown below.

f(6) = g(5) = f(4) + 5 = g(3) + 5 = f(2) + 3 + 5 = g(1) + 3 + 5 = f(0) + 1 + 3 + 5 = 0 + 1 + 3 + 5 = 9

Question 27

When a subclass has a method with the same signature as its superclass, the subclass method overrides the superclass method. This makes Answers A and B incorrect.

The initialization statement for v declares it as a Vehicle reference to an object of type Car. When the reference type differs from the object type, the reference type determines what methods can be run and the object type determines what methods are run.

When setPrice is run on v the object type (Car) determines what method is run (the one from Car). The reference type (Vehicle) determines only that the method can be run.

The setPrice method from Car is run.

I often explain this to my students as the most specific method possible gets run. Car is more specific than Vehicle so the method from Car is run.

Question 28

Method recur has a base case of n == 0. The recursive call is made with n – 2. When n is odd, the base case will never be reached.

The question asks what happens when recur(7) is called. Infinite recursion will occur since the recursive calls will never reach the base case (recur(7) calls recur(5) calls recur(3) calls recur(1) calls recur(-1) and so on.

The position of the call to print, relative to the recursive call to recur determines whether many numbers will be printed or none at all. Since the call to print is after the recursive call, no numbers will be printed.

Question 29

Whenever you are asked to perform arithmetic with numbers in a base other than 10, convert the numbers to base 10, perform the required operations, the convert the numbers back to the required base. My Base Conversion Practice Problem demonstrates a simple algorithms to convert between base 10 and any other base.

10016 = 25610 and 1016 = 1610.

256 – 16 = 240. The answers are already in base 10, so there is no need to convert the answer to another base.

Question 30

Although the answers refer to BoundedIntArray and its instance fields, none of the answers actually depend on its implementation. The question asks why instance fields should be private.

Answer A is incorrect because objects can be initialized regardless of whether their instance fields are public or private.

Answer B is incorrectly states that code that depends on BoundedIntArray can be tested before BoundedIntArray has been completed. Although such code can be written before BoundedIntArray has been completed, it cannot be tested because it cannot be run.

Answer C correctly states that client code could remain unchanged if the implementation of BoundedIntArray is changed. What this means is that client code that uses only the public methods of BoundedIntArray would not need to be changed if BoundedIntArray was changed to use an ArrayList<Integer> instead of an int[]. If the instance fields were public and client code referred to them directly, the client code would have to be changed if the instance fields were changed.

Answers D and E incorrectly suggest that public methods cannot access private instance fields.

Question 31

The first line initializes arr1 to refer to a list of 100 elements with 5 as the index of the first element. arr1 is indexed from 5 to 104.

The second line initializes arr2 to refer to a list of 100 elements with 0 as the index of the first element. arr2 is indexed from 0 to 99.

Question 32

An interface is nothing more than a list of methods required in classes that implement the interface. An interface does not contain implementations of methods, instance fields, or constructors. It cannot be instantiated (see Question 33).

Abstract methods do not contain implementation. None of the methods of an interface contain implementations, so they are all abstract. The abstract modifier is not necessary for interface methods because they must be abstract.

Question 33

Case I is obviously legal. An Integer reference is initialized to refer to a correctly constructed Integer object.

Case II is also legal. Integer implements the Comparable interface, so a Comparable reference can refer to an Integer object.

Case III is not legal. Comparable is an interface and cannot be instantiated. new Comparable(7) attempts to instantiate (make an instance of) Comparable.

Question 34

Answer A is incorrect because there is no constructor in Circle that accepts 3 double values.

Answer B is syntactically incorrect. Placing double values in parentheses does not construct an object of type Point.

Answer C correctly constructs an object of type Point and correctly passes the object and a double to the Circle constructor.

Answers D and E incorrectly access private instance fields of Circle

Question 35

Since the method is intended to be inside the Circle class, it will already have access to the center and radius of the circle. The only parameter needed is an object of type Point. The method should return true if the point is inside the circle, false otherwise. The method should return a boolean.

Question 36

The method is a variant of selection sort that sorts the array in non-decreasing (increasing) order starting from the end of the array.

Each iteration of the outer for loop finds the largest value in the unsorted portion of the array and swaps it with the element at position pass. After 2 iterations of the outer for loop, the 2 largest elements of the array will have been swapped with the last 2 elements of the array.

Although answer C contains the 2 largest elements in the correct positions, the elements previously at those positions have not been correctly swapped into the original positions of the 2 largest elements.

Question 37

You are asked to give code that will determine if the value at index exceeds the current maximum and to update the current maximum.

The maximum is stored as an index (posOfMax) not as a value. To check if the value at index is larger, the condition would be numbers[index] > numbers[posOfMax]. To update the maximum, the statement would be posOfMax = index;

Question 38

When an element is removed from an ArrayList all subsequent elements are shifted down. removeDups incorrectly increments k regardless of whether an element is removed. Whenever an element is removed, removeDups skips the next element. If the next element is also a duplicate, it will not be removed.

When removeDups is run on the given array, the resulting array is 2 7 5 5 6 3 3.

Question 39

To correct the issue identified in Question 38, k should only be incremented if nothing was removed. In other words, k++ should be in an else statement.

Question 40

There are 6 rows of output so the outer loop must run 6 times. This eliminates Answers D and E.

In Answer A, the inner loop incorrectly stops at k < j. The inner loop runs one less time than necessary for each run of the outer loop.

In Answer B, the inner loop incorrectly prints j instead of k. Each row would contain the same value repeated multiple times.

Get AP CS Help

Leave a Reply