2004 AP Computer Science AB Exam Multiple Choice Explanations for A level questions

The College Board released the complete 2004 AP Computer Science AB 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 A level questions from the 2004 AP Computer Science AB 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 A level questions from the 2004 AP CS AB Exam Multiple Choice with AP CS Tutor Brandon Horn.

Recommended Practice Topics

Question 1

A reference of type Animal points to an object of type Tiger.

The reference type determines what methods can be run. The object type determines whether a cast works.

Case I will compile because eat is a method of Animal (the reference type).

Case II will not compile because roar is not a method of Animal.

Case III will compile because the cast changes the reference type to Tiger. The cast works because they object type is Tiger.

Question 2

The == operator is used to determine whether 2 references point to the exact same object.

(The == operator is also used to check whether a reference points to null.)

Question 3

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

Question 7

After line 1 the value of x will be twice its original value.

Line 2 is equivalent to 2x + 2x and results in x having a value 4 times its original value.

Line 3 is equivalent to 4x + 4x and results in x having a value 8 times its original value.

Question 9

All 3 classes have a doNothing method. Example1 and Example2 inherit doNothing from Example0. Any of the calls below are legal with respect to the implicit parameter (the reference before the dot (.)).

e0.doNothing(…)
e1.doNothing(…)
e2.doNothing(…)

The first explicit parameter must be of type Example1 or any subclass. The second explicit parameter must be of type Example2.

Answer A fails because both explicit parameters (e0, e0) are incorrect.
Answer B fails because the second explicit parameter (…, e1) is incorrect.
Answer C fails because the second explicit parameter (…, e1) is incorrect.
Answer D fails because both explicit parameters (e0, e0) are incorrect.
Answer E is a correct call because both explicit parameters (e2, e2) are correct.

Question 10

Each time the loop runs it removes 1 value from the front of the list and adds it to the end of the list. The loop runs 3 times.

The state of the list at the end of the loop is shown below for each value of k for which the loop runs.

0: [9, 7, 8, 4, 3, 6, 11, 1, 12]
1: [7, 8, 4, 3, 6, 11, 1, 12, 9]
2: [8, 4, 3, 6, 11, 1, 12, 9, 7]

The loop stops when k == 3.

Question 13

The loop traverses a single column from mat. Specifically, the loop traverses column 3. Each value in column 3 is added to sum. At the end of the loop sum contains 20 (2 + 4 + 6 + 8).

The statement mat[row][dim – 1] makes it clear that the loop is traversing a column (rather than a row). The value of row varies while the values of dim – 1 remains constant.

To traverse a row instead the row number would remain constant and the column number would vary.

Question 14

maxHelper makes a recursive call on Line 1. maxHelper immediately makes a recursive call regardless of the value of numValues. The method never works as intended because any call to it results in infinite recursion.

Question 15

maxHelper is missing a base case. The base case must appear before Line 1.

Answers C, D and E can be eliminated because they place the base case after the recursive call.

Answer A incorrectly stops when there are 0 elements. A method that returns the largest value cannot stop when there are 0 elements because there is no reasonable return value.

Answer B correctly stops when there is 1 element and correctly returns that element.

Question 16

A class that contains an abstract method must be abstract.

Abstract classes can declare constructors. (Abstract classes can also contain instance fields and non-abstract methods.)

An abstract class cannot be instantiated. For example, the code new AbstractGrid() will not compile because the AbstractGrid class is abstract.

Question 17

If the last names are not equal Name objects are ordered by last name. If the last names are equals Name objects are ordered by first name.

Case I incorrectly returns the sum of the results of calling compareTo on the last names and on the first names. Adding together the results of the compareTo calls results in a value that means nothing. (Case I does work for some situations but it does not work in general.)

Case II checks if the last names are equal using compareTo. If the last names are equal Case II correctly returns the result of comparing the first names. If the last names are not equal Case II correctly returns the result of comparing the last names. Case II works as intended.

Case III uses the same correct logic as Case II. Case III correctly uses the equals method to determine whether the last names are equal. Case III also works as intended.

Question 18

The outer loop skips the first row and visits every other row. The inner loop visits each column (within each row). The if statement will be true for all but 1 value per row. The value at each position for which the if statement is false will be set to the value of the position above it.

For example, consider row 1. The 1 at position [1][0] will be replaced with 4 from position [0][0]. The if statement will be false for position [1][1] so the 8 will not be changed. The 7 at position [1][2] will be replaced with 3 from position [0][2]. The 5 will be replaced with 4 and the 3 will be replaced with 2.

Question 28

The loop stops when either of its conditions is false. When the assertion statement is reached one of the conditions below will be true.

k == seq.length
seq[k – 1] >= seq[k]

These conditions are the opposites of the conditions in the loop. Since only one of the conditions must be true they should be combined with || (Boolean or).

k == seq.length || seq[k-1] >= seq[k]

Question 31

mystery is a correct iterative version of binary search. If this is recognized it is possible to determine how many calls are made without tracing the method line by line. The values of low and high are shown below for each call.

0, 7
0, 2
2, 2
2, 1

There are a total of 4 calls including the initial call.

Alternatively, this method can be treated as a mystery and traced like any other recursive method. The trace is shown at the link below.

https://apcomputersciencetutoring.com/exam-review/2004-ap-cs-ab-31-solution/

Question 33

The base case for mystery occurs when the if statement is false. The if statement is false when r == 0 && c == 0.

r starts at 2 and c starts at 3. Both r and c are decremented by 1 with each recursive call. r and c will never be 0 at the same time.

The base case is never true. mystery ultimately throws an ArrayIndexOutOfBoundsException when mat[-1][0] is accessed.

Leave a Reply