0% found this document useful (0 votes)
26 views37 pages

AP Computer Science 1999 A MC

The document contains an exam for AP Computer Science A with 40 questions covering various programming concepts, primarily in Java. It includes multiple-choice questions about code snippets, functions, and data structures, requiring students to analyze and determine correct outputs or behaviors. The exam is structured to assess understanding of programming logic, syntax, and problem-solving skills within a set time limit.

Uploaded by

JohnLarcile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views37 pages

AP Computer Science 1999 A MC

The document contains an exam for AP Computer Science A with 40 questions covering various programming concepts, primarily in Java. It includes multiple-choice questions about code snippets, functions, and data structures, requiring students to analyze and determine correct outputs or behaviors. The exam is structured to assess understanding of programming logic, syntax, and problem-solving skills within a set time limit.

Uploaded by

JohnLarcile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

COMPUTER SCIENCE A

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.

USE THIS SPACE FOR SCRATCHWORK


1. Consider the following program.

public static void main(String[] args)


{
int k, sum;
Scanner in = new Scanner(System.in);
do
{
System.out.print(“Enter a nonnegative number to sum, “);
System.out.print(“negative number to quit:”);
k = in.nextInt();
if (k > 0)
{
sum += k;
}
} while (k >= 0);
System.out.println(sum);
}

The program contains the incorrect use of an uninitialized variable.


Which of the following is the first line in which this occurs?

(A) k in "k = in.nextInt()"


(B) k in "(k > 0)"
(C) sum in "sum += k"
(D) k in "sum += k"
(E) sum in "System.out.println(sum)"

1999 AP Computer Science A Exam


Questions 2-3 refer to the following function.

public int Add(int x, int y);


//postcondition: returns x + y

public int Multiply(int x, int y);


//postcondition: returns x * y

2. What is the value of the expression Multiply(3, Add(4, 5))?

(A) 12
(B) 17
(C) 23
(D) 27
(E) 60

3. Consider the following expression.

Multiply(2, Add(Multiply(a, b), Add(Multiply(a, c), Multiply(b, c))))

Which of the following corresponds to this expression?

(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

1999 AP Computer Science A Exam


4. Consider the following function.

public int Something(int a, int b)


{
if (b <= 1)
{
return a;
}
else
{
return Something(a, b – 1);
}
}

What value is returned by the call Something(4, 6) ?

(A) 4
(B) 6
(C) 24
(D) 1296
(E) 4096

1999 AP Computer Science A Exam


5. Consider the following incomplete function.

public int Total(int[] scores)


// precondition: The value -999 occurs suomewhere in scores
{
int k = 0;
int sum = 0;

while (scores[k] != -999)


{
<program statements>
}
return sum;
}

Function Total is intended to return the sum of the integers in


parameter scores, beginning with the first integer in scores
and up to, but not including the value -999 (which occurs some-
where in scores). Which of the following code segments could be
used to replace <program statements> so that Total will
work as intended?

(A) sum += scores[k];


k++;

(B) k++;
sum += scores[k];

(C) k++;
sum += k;

(D) sum += k;
k++;

(E) if (scores[k] != -999)


{
sum += scores[k];
}

1999 AP Computer Science A Exam


6. Consider the following code segment.

int[][] M;
Initialize(M); // resizes M to be a square matrix
// and initializes its elements

int sum = 0;
int k;

for (k = 0; k < M.length; k++)


{
sum += M[k][M.length – k – 1];
}

Assume that after the call to Initialize, M represents


the matrix shown below.

0 1 2 3
0 1 1 1 1
1 1 2 3 4
2 2 2 2 2
3 2 4 6 8

What value will sum contain after the code


segment is executed?

(A) 4
(B) 8
(C) 13
(D) 20
(D) 42

1999 AP Computer Science A Exam


Questions 7-8 are based on the following incomplete function.

int Fun(int x, int y)


// precondition: (x * y) ≥ 0
// postcondition: returns a value ≥ 0
{
<body of Fun>
}
Assume that <body of Fun> is replaced with code so
that Fun meets the specification defined by its precondition
and its postcondition.

7. What can be assumed about the value returned by the


call Fun(0, 0) ?

(A) The value is 0.


(B) The value is not equal to 9.
(C) The value is less than or equal to 0.
(D) The value is greater than or equal to 0.
(E) No assumption can be made about the value returned.

8. What can be assumed about the value returned by the call


Fun(-1, 1) ?

(A) The value is -1.


(B) The value is 0.
(C) The value is 1.
(D) The value is greater than or equal to 0.
(E) No assumption can be made about the value returned.

1999 AP Computer Science A Exam


9. Consider the following code segment.

char[] A = new char[100];


char[] B = new char[100];
int k = 0;
while( ((k < A.length) && (A[k] != B[k]))
{
k++;
}

Which of the following must be true after the while loop terminates?

(A) k >= A.length


(B) k < A.length
(C) (k < A.length) && (A[k] != B[k])
(D) (k >= A.length) || (A[k] != B[k])
(E) (K >= A.length) || (A[k] == B[k])

1999 AP Computer Science A Exam


10. Consider the incomplete function PowerOf given below.
The call PowerOf(n, x) should return the quantity nx.

int PowerOf(int base, int power)


// precondition: power ≥ 1
// postcondition: returns basepower
{
int result;

if(<expression1> == 1)
{
result = <expression2> ;
}
else
{
result = <expression3> * PowerOf(base, power – 1);
}
return result;
}

Which of the following could be used to replace <expression1>, <expression2>, and


<expression3> so that PowerOf will work as intended?

<expression1> <expression2> <expression3>

(A) power base result


(B) power base power
(C) power base base
(D) base power result
(E) base power base

1999 AP Computer Science A Exam


11. Assume A is defined as follows.

int[] A = new int[5];

Consider the following code segment.

int k;
for (k = A.length -1; k > 0; k--)
{
A[k] = A[k-1];
}

Assume A contains the following values before the code


segment is executed.

0 1 2 3 4
10 15 20 25 30

What values will A contain after the code segment is executed?

(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

1999 AP Computer Science A Exam


Questions 12-13 concern the following data structure, designed to
store information about hiking trails in the United States.

public class Trail


{
public String name; // name of trail
public String state; // location of trail
public double distance; // length of trail in kilometers
public boolean[] goodMonths;
// goodMonths[k] == true if this trail is good for hiking
// in month k; otherwise, goodMonths[k] == false

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) goodMonths[j] && goodMonths[k]


(B) goodMonths[j] || goodMonths[k]
(C) (goodMonths == j) || (goodMonths == k)
(D) (goodMonths == j) && (goodMonths == k)
(E) goodMonths[j] == goodMonths[k]

1999 AP Computer Science A Exam


13. Consider the incomplete function PrintTrails given
below. PrintTrails should print the names of the trails
in its parameter trailArray that are good for hiking in
the month specified by parameter month.

void PrintTrails(Trail[] trailArray, int month)


{
int k;
for (k = 0; k < trailArray.length; k++)
{
<loop body>
}
}

Which of the following could be used to replace <loop body>


so that PrintTrails works as intended?

(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);
}

1999 AP Computer Science A Exam


14. Consider the following incomplete function.

int Mystery(int k)
{
if (k <= 0)
{
return 0;
}
else
{
return (<missing code>);
}
}

Which of the following could be used to replace <missing code>


so that the value of Mystery(5) is 15 ?

(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)

1999 AP Computer Science A Exam


15. Assume that an array contains 100 integers sorted in increasing
order. Two alternatives to search the array for a particular integer
are a sequential and a binary search. When searching for a value
that is in the array, which of the following best characterizes the
greatest number of items in the array that will be examined during
each kind of search?

Sequential Binary

(A) 100 1
(B) 100 7
(C) 100 50
(D) 50 7
(E) 50 25

1999 AP Computer Science A Exam


16. Consider the following code segment.

List<String> animals = new ArrayList<String>();

animals.add ( "dog”);
animals.add("cat”);
animals.add("snake”);
animals.set(2, "lizard”)
animals.add(1, "fish”)
animals.remove (3);
System.out.println (animals)

What is printed as a result of executing the code segment?

(A) [dog, fish, cat]


(B) [dog, fish, lizard]
(c) [dog, lizard, fish]
(D) [fish, dog, cat]
(E) The code throws an ArrayIndexOutOfBoundsException exception.

17. Consider the following method.

public static void mystery(List<Integer> nums)


{
for (int k = 0; k < nums.size(); k++)
{
if (nums.get(k).intValue() == 0)
{
nums.remove(k);
}
}
}

Assume that a List<Integer> values initially contains the following Integer values.

[0, 0, 4, 2, 5, 0, 3, 0]

What will values contain as a result of executing mystery (values) ?

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

1999 AP Computer Science A Exam


18. Consider the following output.

11111
2222
333
44
5

Which of the following code segments will produce this output?

(A) for (int j = 1; j <= 5; j++)


{
for (int k = 1; k <= 5; k++)
{
System.out.print(j + " ");
}
System.out.println();
}

(B) for (int j = 1; j <= 5; j++)


{
for (int k = 1; k <= j; k++)
{
System.out.print(j + " ");
}
System.out.println();
}

(C) for (int j = 1; j <= 5; j++)


{
for (int k = 5; k >= 1; k—-)
{
System.outprint(j + " ");
}
System.out.println();
}

(D) for (int j = 1; j <= 5; j++)


{
for (int k = 5; k >= j; k--)
{
System.out.print(j + " ");
}
System.out.println();
}

(E) for (int j = 1; j <= 5; j++)


{
for (int k = j; k <= 5; k++)
{
System.out.print(k + " ");
}
System.out.println();
}

1999 AP Computer Science A Exam


Questions 19-20 refer to the following incomplete class declaration.

public class TimeRecord


{
private int hours;
private int minutes; // 0 < minutes < 60
/** Constructs a TimeRecord object.
* @param h the number of hours
* Precondition: h > 0
* @param m the number of minutes
* Precondition: 0 < m < 60
*/
public TimeRecord(int h, int m)
{
hours = h;
minutes = m;
}

/** @return the number of hours


*/
public int getHours()
{ /* implementation not shown */ }

/** @return the number of minutes


* Postcondition: 0 < minutes < 60
*/
public int getMinutes()
{ /* implementation not shown */ }

/** Adds h hours and m minutes to this TimeRecord.


* @param h the number of hours
* Precondition: h > 0
* @param m the number of minutes
* Precondition: m > 0
*/
public void advance(int h, int m)
{
hours = hours + h;
minutes = minutes + m;
/* missing code */
}

// Other methods not shown


}

1999 AP Computer Science A Exam


19. Which of the following can be used to replace /* missing code */ so that advance will
correctly update the time?

(A) minutes = minutes % 60;

(B) minutes = minutes + hours % 60;

(C) hours = hours + minutes / 60;


minutes = minutes % 60;

(D) hours = hours + minutes % 60;


minutes = minutes / 60;

(E) hours = hours + minutes / 60;

20. Consider the following declaration that appears in a class other than TimeRecord.

TimeRecord[] timeCards = new TimeRecord[l00];

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.

TimeRecord total = new TimeRecord(0,0);

for (int k = 0; k < timeCards.length; k++)


{
/* missing expression */
}

Which of the following can be used to replace /* missing expression */ so that the code
segment will work as intended?

(A) timeCards[k].advance()

(B) total += 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())

1999 AP Computer Science A Exam


21. The program for a video game will use several graphics routines.
The program design team plans to place these routines in a graphics
class, which can be compiled separately from the video game program.
Which of the following would be an advantage of this plan?

I. The graphics routines can be tested independently of the video


game program, thus making it easier to locate errors in both the
graphics routines and the video game program.
II. The programmers assigned to write the video game program
can focus on the issues of that program without spending time
considering how the graphics routines will be implemented.
III. The graphics class will be available for use in other programs.

(A) I only
(B) I and II only
(C) I and III only
(D) II and III only
(E) I, II, and III

1999 AP Computer Science A Exam


22. Consider the following code segment to print a calendar.

int month, year;


System.out.print("Enter year: ");
Scanner in = new Scanner(System.in);
year = in.nextInt();
for (month = 1; month <= 12; month++)
{
PrintHeading(month, year);
PrintDays(month, year);
}

Consider the following function.

public void PrintDays(int month, int year)


{
int day;
PrintSpaces(month, year); // indent the first week of the month
for (day = 1; day <= NumDaysIn(month, year); day++)
{
System.out.print(day+" ");
if (EndOfWeek(day, month, year))
{
System.out.println();
}
}
}

Suppose that when the program is run, every month is printed


correctly except for February, for which only a heading and
some white space is printed. Of the following functions, which
is most Likely to contain the error?

(A) NumDaysIn
(B) PrintSpaces
(C) EndOfWeek
(D) PrintHeading
(E) PrintDays

1999 AP Computer Science A Exam


23. Consider the following code segment.

int row, col;


int sum = 0;
int[][] A;
Initialize(A); // sizes A and initializes its elements

for (row = 0; row < A.length; row++)


{
for (col = 0; col < A[row].length; col++)
{
sum += A[row][col];
}
}

Which of the following best describes the result of executing


the code segment?

(A) Each element in the two-dimensional array A contains the


value 0.
(B) Each element in the two-dimensional array A contains the
sum of its row number and its column number.
(C) Each element in the two-dimensional array A contains the
sum of all preceding elements in two-dimensional array A.
(D) The variable sum contains the sum of the values in the
two-dimensional array A.
(E) The variable sum contains the value row * col.

1999 AP Computer Science A Exam


24. Consider the following code.

int[][] M;
Initialize(M); // resizes M to be a square matrix
// and initializes its elements

Which of the following code segments correctly sets a diagonal


of the two-dimensional array M to contain all zeroes?

(A) int row = 0;


int col = 0;
while ((row < M.length) && (col < M[0].length))
{
M[row][col] = 0;
row++;
row = col;
}

(B) int row = 0;


int col = 0;
while (row < M.length)
{
M[row][col] = 0;
row++;
col = row;
col++;
}

(C) int row = 0;


int col = 0;
while (row < M.length)
{
M[row][col] = 0;
row++;
}

(D) int row;


for (row = 0; row < M.length; row++)
{
M[row][row] = 0;
}

(E) int row;


for( row = 1; row <= M.length; row++)
{
M[row][row] = 0;
}

1999 AP Computer Science A Exam


25. For each hour of the day, a weather station records temperature
using integer value, and pressure, wind speed, and wind direction
using value of type double. Of the following definitions of
dailyRecord, which would be most suitable for recording
these weather readings for one day?

(A) int[][] dailyRecord = new int[24][4];

(B) int[] dailyRecord = new int[96];

(C) public class WeatherInfo


{
public int temperature;
public double pressure;
public double windSpeed;
public double windDir;
}
WeatherInfo dailyRecord = new WeatherInfo();

(D) public class WeatherInfo


{
public int temperature;
public double pressure;
public double windSpeed;
public double windDir;
}
WeatherInfo[] dailyRecord = new WeatherInfo[24];

(E) public class WeatherInfo


{
public int temperature;
public double pressure;
public double windSpeed;
public double windDir;
}
WeatherInfo[] dailyRecord = new WeatherInfo[366 * 24];

1999 AP Computer Science A Exam


26. Java classes can include both public and private instance
variables and functions. Which of the following statements
represents the best design decision regarding the public and
private sections of a class?

(A) All instance variables should be public to make it easier for


client programs to use such data.
(B) All functions should be public to facilitate future changes to
parameter lists of functions.
(C) All instance variables should be private to minimize the
dependency between client programs and the manner
in which data is stored in the class.
(D) Some functions should be private to minimize memory
usage.
(E) All instance variables should be public and all functions
should be private to make it easier to modify the class
without requiring changes in the code of the client.

1999 AP Computer Science A Exam


27. Consider the following function.

public boolean SomethingDifferent(bool p, bool q)


{
return ((p || q) && !(p && q));
}

What does function SomethingDifferent return?

(A) SomethingDifferent always returns false.


(B) SomethingDifferent always returns true.
(C) SomethingDifferent returns true whenever p is false.
(D) SomethingDifferent returns true whenever q is false.
(E) SomethingDifferent returns true whenever p is not equal to q.

1999 AP Computer Science A Exam


28. Which of the following statements would best characterize
a well-designed program?

I. Functions can be tested independently before


integrating them into the final program.
II. Client programs know about, and take advantage
of, implementation details of abstract data types.
III. The algorithmic details of the abstract data types
can be altered without changing client routines.

(A) I only
(B) II only
(C) I and II
(D) I and III
(E) II and III

29. Consider the following instance variable and method.

private int[] numbers;

/** Precondition: numbers contains int values in no particular


order.
*/
public int mystery(int num)
{
for (int k = numbers.length - 1; k >= 0; k--)
{
if (numbers[k] < num)
{
return k;
}
}
return -1;
}

Which of the following best describes the contents of numbers after the following statement has been
executed?

int m = mystery(n);

(A) All values in positions 0 through m are less than n.


(B) All values in positions m+i through numbers.length-i are less than n.
(C) All values in positions m+i through numbers.length-i are greater than or equal to n.
(D) The smallest value is at position m.
(E) The largest value that is smaller than n is at position m.

1999 AP Computer Science A Exam


30. Consider the following definitions and code segment.

int[] A = new int[7];


int x;
for (x = 0; x < A.length; x++)
{
A[x] = x;
}
for (x = 0; x < A.length; x++)
{
A[x/3] = A[x];
}
What values will A contain after the code segment is executed?

(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

1999 AP Computer Science A Exam


31. Assume that A is an array of N integers and that variable k
has a value in the range 0 ≤ k ≤ N. Also assume that the
following assertation is true:

for all j, 0 ≤ j < k, A[j] < A[j + 1]

Which of the following is a valid conclusion?

(A) All elements of A are in increasing order.


(B) All elements of A are in decreasing order.
(C) Elements 0 through k of A are in increasing order.
(D) Elements 0 through k of A are in decreasing order.
(E) The smallest value in A is stored in A[0].

1999 AP Computer Science A Exam


32. Consider the following code segment. Assume that neither
<condition 1>, <condition 2>, nor <condition 3>
changes the value of k.

int k = 0;
if (<condition 1>)
{
k++;
}
if (<condition 2>)
{
k++;
}
if (<condition 3>)
{
k++;
}

What are the possible final value of k after the code


segment executes?

(A) 0 only
(B) 1 only
(C) 0 or 1 only
(D) 1, 2, or 3 only
(E) 0, 1, 2, or 3

1999 AP Computer Science A Exam


33. Consider designing a data structure to represent the positions
of 50 game pieces on a 100 x 100 gameboard. (The position of
a game piece is the row and column number of the square that
it is on.) Two alternatives are described below.

Method 1. Use a two-dimensional array of boolean values


indexed by row and column number, where each
array element represents one square of the gameboard.
If there is a game piece on the square, then the array
element is true; otherwise the array element is false.

Method 2. Use a one-dimensional array in which each element


represents the position of one game piece (i.e., the row
and column number of the square that it is on).

Which of the following is true?

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

1999 AP Computer Science A Exam


34. Consider the following function.

public void Print(int count)


{
int k;
Scanner in = new Scanner(System.in);

if (count > 0)
{
k = in.nextInt();
Print(count - 1);
System.out.println(k);
}
}

Of the following, which best describes what is printed as a


result of the call Print(10) ?

(A) Nothing is printed because a run-time error occurs.


(B) Nothing is printed because the if condition never
evaluates to true.
(C) Ten integers are printed in the same order in which
they were read.
(D) Ten integers are printed in the reverse order in which
they were read.
(E) Only the nonzero values that were read are printed; they
are printed in the same order in which they were read.

1999 AP Computer Science A Exam


35. Consider the following code segment.

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]);
}

Assume that function Swap interchanges the values of its parameters.


Which of the following best characterizes the effect of the for loop?

(A) It sorts the elements of A.


(B) It reverses the elements of A.
(C) It reverses the order of the first half of A and leaves the second
half unchanged.
(D) It reverses the order of the second half of A and leaves the first
half unchanged.
(E) It leaves all of the elements of A in their original order.

1999 AP Computer Science A Exam


36. The following class is intended to sort an array of unique
integers in increasing order using the quicksort algorithm.
public class QuickSort
{
private static int pivotPos;
public static void Partition(int[] A, int first, int last)
{
// code
}
public static void sort(int[] A, int first, int last)
{
// If the subarray has at least 2 elements, partition
// and recursively sort the two partitions.
if (first < last)
{
Partition(A, first, last);
<statements>
}
}
}

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.

Which of the following can be used to replace <statements> so that


QuickSort will work as intended?

(A) sort(A, first, pivotPos + 1);


sort(A, pivotPos - 1, last);
(B) sort(A, last, pivotPos);
sort(A, pivotPos, first);
(C) sort(A, first, pivotPos - 1);
sort(A, last, pivotPos + 1);
(D) sort(A, first, pivotPos - 1);
sort(A, pivotPos + 1, last);
(E) QuickSort(A, pivotPos - 1, first);
QuickSort(A, last, pivotPos + 1);

1999 AP Computer Science A Exam


Questions 37-38 refer to the following information.

Consider the following class declaration.

private class Restaurant


{
private String myName; // the restaurant's name
// other variables not shown

public String name()


{
// returns the restaurant's name
}
public double price()
{
// returns the price of a meal
// (all meals in a specific restaurant
// cost the same)
}
public int capacity()
{
// returns the maximum number of
// customers the restaurant can
// serve at one time
}
//other methods not shown
}

1999 AP Computer Science A Exam


37. Assume that a client program declares and initializes rList
as follows:

Restaurant[] rList = null;


Initialize(rList); //resizes rList and initializes its elements

Which of the following code segments correctly prints the names


of all the restaurants whose meal price is under $10.00 ?

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

1999 AP Computer Science A Exam


38. Consider the following function.

void PrintSomeRestaurants(Restaurant[] rList)


// precondition: rList.length > 0
{
int r;
int numRests = rList.length;
double sum = 0.0;
double average;

for (r = 0; r < numRests; r++)


{
sum += rList[r].price();
}
average = sum / numRests;
for (r = 0; r < nuRests; r++)
{
if((rList[r].capacity() >= 50) && (rList[r].price() < average))
{
System.out.rpintln(rList[r].name());
}
}
}

Of the following, which best describes the behavior of


PrintSomeRestaurants ?

(A) It prints the name of the firs restaurant whose meal


price is below the average of all restaurants.
(B) It prints the name of the first restaurant whose capacity
is at least 50 and whose meal price is less than the
average for the meal prices for all restaurants.
(C) It prints the names of all restaurants whose capacity is
at least 50 and whose meal price is below the average
for the meal prices for all restaurants.
(D) It prints the average of the meal prices of all restaurants.
(E) It prints the average of the meal prices of all restaurants
whose capacity is at least 50.

1999 AP Computer Science A Exam


39. Consider the following code segment.

x = !y;
y = !x;

Assume that x and y are initialized variables of type boolean.


Which of the following statements is (are) true?

I. The final value of x is the same as the initial value of x.


II. The final value of y is the same as the initial value of y.
III. The final value of x is the same as the initial value of y.

(A) I only
(B) II only
(C) III only
(D) I and II
(E) II and III

1999 AP Computer Science A Exam


40. Assume that functions RowSum and ColSum, declared below, have been implemented correctly.

int RowSum(int[][] A, int k);


// precondition: A is a square matrix, 0 ≤ k < A.length
// postcondition: returns the sum of the elements in
// row k of array A

int ColSum(int[][] A, int k);


// precondition: A is a square matrix, 0 ≤ k < A[0].length
// postcondition: returns the sum of the elements in
// column k of array A

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);

for (k = 0; k < A.length; k++)


{
if (<condition>)
{
<statement 1>
}
}
<statement 2>
}

Which of the following could be used to replace <condition>, <statement 1>, and
<statement 2> so that MagicSquare will work as intended?

<condition> <statement 1> <statement 2>


(A) ((RowSum(A,k) != sum) return(false); return(true);
|| (ColSum(A,k) != sum))

(B) ((RowSum(A,k) != sum) return(false); return(true);


&& (ColSum(A,k) != sum))

(C) ((RowSum(A,k) != sum) return(true); return(false);


&& (ColSum(A,k) != sum))

(D) ((RowSum(A,k) == sum) return(true); return(false);


|| (ColSum(A,k) == sum))

(E) ((RowSum(A,k) == sum) return(true); return(false);


&& (ColSum(A,k) == sum))
1999 AP Computer Science A Exam

You might also like