Question 1

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

The reference/variable type determines what methods can be run. The object/instance 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 the 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 stores null.)

Question 3

The method can be traced using recursive method tracing.

See a step by step trace of #3.

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

Example0 explicitly implements the doNothing method. Example1 and Example2 inherit doNothing from Example0. Any of the calls below are legal with respect to the implicit parameter.

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.

Answers A and D fail because both explicit parameters (e0, e0) are incorrect.

Answers B and C fail because the second explicit parameter (..., e1) is incorrect. An Example1 cannot be substituted for an Example2.

Answer E is a correct call because both explicit parameters (e2, e2) are correct. An Example2 can be substituted for an Example1`.

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 value of dim - 1 remains constant.

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

See Intro to 2D arrays for additional details.

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.

This question can be solved using the technique at analyzing recursive methods.

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 variables and non-abstract methods.) Constructors in abstract classes are called by subclass constructors to initialie the instance variables.

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 equal 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.

See Strings on the AP CS A Exam and compareTo on the AP CS A Exam for additional discussion.

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 condition will be true for all but 1 value per row. The value at each position for which the if condition 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 condition 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 || (logical or).

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

Question 31

mystery is a correct recursive 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, high, and mid are shown below for each call.

low high mid
0 7 3
0 2 1
2 2 2
2 1 not computed

There are a total of 4 calls including the initial call. The base case is low > high (zero elements remaining), not low == high (1 element remaining). When low and high are both 2, the mid is calculated as usual and mystery is called again.

Alternatively, the method can be traced using recursive method tracing.

See a step by step trace of #31.

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 throws an ArrayIndexOutOfBoundsException when mat[-1][0] is accessed.

This is an example of recursive method analysis

Alternatively, the method can be traced using recursive method tracing.

See a step by step trace of #33.

Comments

Comment on A level from 2004 AP CS AB MC Explanations