AP Computer Science 1999 A MC
AP Computer Science 1999 A MC
SECTION I
Time – 1 hour and 15 minutes
Number of questions—40
Percent of total grade—50
Directions: Determine the answer to each of the following questions or incomplete statements, using the
available space for any necessary scratchwork. Then decide which is the best of the choices given and fill
in the corresponding oval on the answer sheet. No credit will be given for anything written in the
examination booklet. Do not spend too much time on any one problem.
Note: Assume that the standard libraries (e.g., java.util.*, java.lang.*, java.io.*) are included in any
programs that use the code segments provided in individual questions. A Quick Reference to common
Java classes is provided.
(A) 12
(B) 17
(C) 23
(D) 27
(E) 60
(A) 2 * a * b + a * c + b * c
(B) a * b + a * c + b * c + 2
(C) 2 * (a * b + (a * c + b * c))
(D) a * b + (a * c + b * c)
(E) a * b + (a * c + b * c) * 2
(A) 4
(B) 6
(C) 24
(D) 1296
(E) 4096
(B) k++;
sum += scores[k];
(C) k++;
sum += k;
(D) sum += k;
k++;
int[][] M;
Initialize(M); // resizes M to be a square matrix
// and initializes its elements
int sum = 0;
int k;
0 1 2 3
0 1 1 1 1
1 1 2 3 4
2 2 2 2 2
3 2 4 6 8
(A) 4
(B) 8
(C) 13
(D) 20
(D) 42
Which of the following must be true after the while loop terminates?
if(<expression1> == 1)
{
result = <expression2> ;
}
else
{
result = <expression3> * PowerOf(base, power – 1);
}
return result;
}
int k;
for (k = A.length -1; k > 0; k--)
{
A[k] = A[k-1];
}
0 1 2 3 4
10 15 20 25 30
(A) 10 10 10 10 10
(B) 10 10 15 20 25
(C) 10 15 20 25 30
(D) 15 20 25 30 30
(E) 25 25 25 25 25
public Trail() {
// constructor initialized goodMonths to length 12
}
}
12. Assume that the integers j and k represent valid months. Which of the
following expressions in a Trail method would always evaluate to true if
the trail represented by T is good for hiking during month j or k ?
(A) if (trailArray[k].goodMonths[month])
{
System.out.println(trailArray[k].name);
}
(B) if (trailArray[month].goodMonths[k])
{
System.out.println(trailArray[month].name);
}
(C) if (trailArray[k] == month)
{
System.out.println(trailArray[k].name);
}
(D) if (trailArray.goodMonths == month)
{
System.out.println(trailArray.goodMonths.name);
}
(E) if (trailArray.goodMonths[k] == month)
{
System.out.println(trailArray.goodMonths[k].name);
}
int Mystery(int k)
{
if (k <= 0)
{
return 0;
}
else
{
return (<missing code>);
}
}
(A) k + Mystery(k – 1)
(B) k * Mystery(k – 1)
(C) Mystery (k – 1)
(D) Mystery(k + 1)
(E) Mystery(k – 1) * Mystery(k + 1)
Sequential Binary
(A) 100 1
(B) 100 7
(C) 100 50
(D) 50 7
(E) 50 25
animals.add ( "dog”);
animals.add("cat”);
animals.add("snake”);
animals.set(2, "lizard”)
animals.add(1, "fish”)
animals.remove (3);
System.out.println (animals)
Assume that a List<Integer> values initially contains the following Integer values.
[0, 0, 4, 2, 5, 0, 3, 0]
(A) [0, 0, 4, 2, 5, 0, 3, 0]
(B) [4, 2, 5, 3]
(c) [0, 0, 0, 0, 4, 2, 5, 3]
(D) [0, 4, 2, 5, 3]
(E) The code throws an ArraylndexOutOfBoundsException exception.
11111
2222
333
44
5
20. Consider the following declaration that appears in a class other than TimeRecord.
Assume that timeCards has been initialized with TimeRecord objects. Consider the following code
segment that is intended to compute the total of all the times stored in timeCards.
Which of the following can be used to replace /* missing expression */ so that the code
segment will work as intended?
(A) timeCards[k].advance()
(C) total.advance(timeCards[k].hours,
timeCards[k].minutes)
(D) total.advance(timeCards[k].getHours(),
timeCards[k].getMinutes())
(E) timeCards[k].advance(timeCards[k].getHours(),
timeCards[k].getMinutes())
(A) I only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III
(A) NumDaysIn
(B) PrintSpaces
(C) EndOfWeek
(D) PrintHeading
(E) PrintDays
int[][] M;
Initialize(M); // resizes M to be a square matrix
// and initializes its elements
(A) I only
(B) II only
(C) I and II
(D) I and III
(E) II and III
Which of the following best describes the contents of numbers after the following statement has been
executed?
int m = mystery(n);
(A) 0 0 1 1 1 2 2
(B) 0 1 2 3 4 5 6
(C) 1 2 0 1 2 0 1
(D) 2 3 4 5 6 0 1
(E) 2 5 6 3 4 5 6
int k = 0;
if (<condition 1>)
{
k++;
}
if (<condition 2>)
{
k++;
}
if (<condition 3>)
{
k++;
}
(A) 0 only
(B) 1 only
(C) 0 or 1 only
(D) 1, 2, or 3 only
(E) 0, 1, 2, or 3
(A) Method 1 is not suitable if two game pieces can occupy the same
square of the gameboard.
(B) Method 2 is not suitable if two game pieces can occupy the same
square of the gameboard.
(C) Printing the positions of all game pieces can be done more
efficiently by using Method 1 than by using Method 2.
(D) Determining whether there is a game piece on a particular square
(given the row and column numbers) can be done more efficiently
by using Method 2 than by using Method 1.
(E) Removing the game piece from a particular square (given its row
and column numbers) can be done more efficiently by using
Method 2 than by using Method 1.
if (count > 0)
{
k = in.nextInt();
Print(count - 1);
System.out.println(k);
}
}
int[] A;
Initialize(A); // resizes A and initializes its elements
int k;
for (k = 0; k < A.length; k++)
{
Swap(A[k], A[A.length - k - 1]);
}
The variables first and last are the indices of the first and last elements
in the subarray of array A to be sorted. The function Partition
performs the task of splitting the array into two subarrays around a pivot
point, pivotPos, chosen by Partition. After the call to Partition,
the subarray from A[first] to A[pivotPos - 1] contains integers
that are less than A[pivotPos], and the subarray from A[pivotPos + 1]
to A[last] contains integers greater than A[pivotPos]. The element
A[pivotPos] is in its final sorted position.
I. int r;
for (r = rList.length - 1; r >= 0; r--)
{
if (rList[r].price() < 10.00)
{
System.out.println(rList[r].name());
}
}
II. int r;
for (r = 0; r < rList.length; r++)
{
if (rList[r].price() < 10.00)
{
System.out.println(rList[r].name());
}
}
III. int r;
for (r = 0; r < rList.length; r++)
{
if (rList[r].price() < 10.00)
{
System.out.println(rList[r].myName);
}
}
(A) I only
(B) II only
(C) I and II only
(D) II and III only
(E) I, II, and III
x = !y;
y = !x;
(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III
Consider the partially written function MagicSquare, shown below. MagicSquare should return
true if and only if every row and every column of its parameter A sums to the save value.
boolean MagicSquare(int[][] A)
// precondition: A is a square matrix
{
int k;
int sum = RowSum(A, 0);
Which of the following could be used to replace <condition>, <statement 1>, and
<statement 2> so that MagicSquare will work as intended?