0% found this document useful (0 votes)
533 views

AP Comp Sci A Practice FRQ 1

This document contains instructions for a practice exam with 4 questions related to computer science. Question 1 involves writing a method to reverse the elements of a one-dimensional array and methods to reverse elements of a two-dimensional array. Question 2 provides an incomplete ArrayUtil and Matrix class and asks to complete methods to reverse arrays and matrices.
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)
533 views

AP Comp Sci A Practice FRQ 1

This document contains instructions for a practice exam with 4 questions related to computer science. Question 1 involves writing a method to reverse the elements of a one-dimensional array and methods to reverse elements of a two-dimensional array. Question 2 provides an incomplete ArrayUtil and Matrix class and asks to complete methods to reverse arrays and matrices.
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/ 3

Practice Exam One / Diagnostic Test 33

COMPUTER SCIENCE
SECTION II

Diagnostic Test
Time—1 hour and 45 minutes
Number of questions—4
Percent of total grade—50

Directions: SHOW ALL YOUR WORK. REMEMBER THAT


PROGRAM SEGMENTS ARE TO BE WRITTEN IN Java.
Write your answers in pencil only in the booklet provided.
Notes:
• Assume that the classes in the Quick Reference have been imported where
needed.
• Unless otherwise stated, assume that parameters in method calls are not
null and that methods are called only when their preconditions are satis-
fied.
• In writing solutions for each question, you may use any of the accessible
methods that are listed in classes defined in that question. Writing signifi-
cant amounts of code that can be replaced by a call to one of these methods
may not receive full credit.

1. This question manipulates one-dimensional and two-dimensional arrays. In


part (a) you will write a method to reverse elements of a one-dimensional ar-
ray. In parts (b) and (c) you will write methods to reverse elements of a two-
dimensional array.

(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) */ }

//Other methods are not shown.


}

GO ON TO THE NEXT PAGE.


34 Practice Exam

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

Complete method reverseArray below.

/** 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)

(b) Consider the following incomplete Matrix class, which represents a two-
dimensional matrix of integers. Assume that the matrix contains at least
one integer.

public class Matrix


{
private int[][] mat;

/** Constructs a matrix of integers. */


public Matrix (int[][] m)
{ mat = m; }

/** Reverses the elements in each row of mat.


* Postcondition: The elements in each row have been reversed.
*/
public void reverseAllRows()
{ /* to be implemented in part (b) */ }

/** Reverses the elements of mat.


* Postcondition:
* - The final elements of mat, when read in row-major order,
* are the same as the original elements of mat when read
* from the bottom corner, right to left, going upward.
* - mat[0][0] contains what was originally the last element.
* - mat[mat.length-1][mat[0].length-1] contains what was
* originally the first element.
*/
public void reverseMatrix()
{ /* to be implemented in part (c) */ }

//Other instance variables, constructors and methods are not shown.


}

GO ON TO THE NEXT PAGE.


Practice Exam One / Diagnostic Test 35

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.

Before call After call


0 1 2 3 0 1 2 3
0 1 2 3 4 0 4 3 2 1
1 5 6 7 8 1 8 7 6 5
2 9 10 11 12 2 12 11 10 9

In writing reverseAllRows, you must call the reverseArray method in part


(a). Assume that reverseArray works correctly regardless of what you
wrote in part (a).
Complete method reverseAllRows below.

/** Reverses the elements in each row of mat.


* Postcondition: The elements in each row have been reversed.
*/
public void reverseAllRows()

(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

In writing reverseMatrix, you must call the reverseAllRows method in


part (b). Assume that reverseAllRows works correctly regardless of what
you wrote in part (b).
Complete method reverseMatrix below.

/** Reverses the elements of mat.


* Postcondition:
* - The final elements of mat, when read in row-major order,
* are the same as the original elements of mat when read
* from the bottom corner, right to left, going upward.
* - mat[0][0] contains what was originally the last element.
* - mat[mat.length-1][mat[0].length-1] contains what was
* originally the first element.
*/
public void reverseMatrix()

GO ON TO THE NEXT PAGE.

You might also like