AP Comp Sci A Practice FRQ 1
AP Comp Sci A Practice FRQ 1
COMPUTER SCIENCE
SECTION II
Diagnostic Test
Time—1 hour and 45 minutes
Number of questions—4
Percent of total grade—50
(a) Consider the following incomplete ArrayUtil class, which contains a static
reverseArray method.
public class ArrayUtil
{
/** Reverses elements of array arr.
* Precondition: arr.length > 0.
* Postcondition: The elements of arr have been reversed.
* @param arr the array to manipulate
*/
public static void reverseArray(int[] arr)
{ /* to be implemented in part (a) */ }
Write the ArrayUtil method reverseArray. For example, if arr is the array
{2,7,5,1,0}, the call to reverseArray changes arr to be {0,1,5,7,2}.
Diagnostic Test
(b) Consider the following incomplete Matrix class, which represents a two-
dimensional matrix of integers. Assume that the matrix contains at least
one integer.
Write the Matrix method reverseAllRows. This method reverses the ele-
ments of each row. For example, if mat1 refers to a Matrix object, then the
Diagnostic Test
call mat1.reverseAllRows() will change the matrix as shown below.
(c) Write the Matrix method reverseMatrix. This method reverses the ele-
ments of a matrix such that the final elements of the matrix, when read in
row-major order, are the same as the original elements when read from the
bottom corner, right to left, going upward. Again let mat1 be a reference to
a Matrix object. The the call mat1.reverseMatrix() will change the matrix
as shown below.
Before call After call
0 1 0 1
0 1 2 0 6 5
1 3 4 1 4 3
2 5 6 2 2 1