0% found this document useful (0 votes)
109 views7 pages

Unit 8 AP Computer Science Exam

The document provides a practice exam on two-dimensional arrays for AP Computer Science A. It contains 10 multiple choice questions and 1 free response question about representing a battlefield using a two-dimensional boolean array and methods to check for land mines at a location or safely crossing a row.

Uploaded by

shandilyaabhijay
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)
109 views7 pages

Unit 8 AP Computer Science Exam

The document provides a practice exam on two-dimensional arrays for AP Computer Science A. It contains 10 multiple choice questions and 1 free response question about representing a battlefield using a two-dimensional boolean array and methods to check for land mines at a location or safely crossing a row.

Uploaded by

shandilyaabhijay
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

Unit 8 AP Computer Science A Practice Exam

Two-Dimensional Arrays

Section I – Multiple Choice


Optional Time – 20 minutes
10 Questions

1) Which of the following are true 4) Which of the following is the correct
about two-dimensional arrays? declaration of a String two-
dimensional array field meant to
I. All of the elements inside of an store sensitive data?
array must be of the same type or
related types. (A) String[][] data;
II. When creating an array with the (B) private String[][]
keyword new, integers are data;
initialized to 0. (C) public String[][]
III. The first row of an array is data;
located at the index of 1. (D) static String[][]
data;
(A) I only
(B) II only 5) Which of the following is the correct
(C) I and II initialization of a two-dimensional
(D) II and III array that should have 3 rows and 4
(E) I, II, and III columns?

2) Which of the following describes (A) String[][] data = new


how two-dimensional arrays are String[3][4]
configured? (B) String[][] data = new
String[4][3]
(A) Row-major order (C) String[3][4] data =
(B) Column-major order new String[3][4]
(C) Row-minor order (D) String[4][3] data =
(D) Column-minor order new String[4][3]

6) Which of the following is the correct


3) Which of the following is the syntax
code to store the number of columns
to get an element out of a two-
in a two-dimensional array a into the
dimensional array at row x and
nC variable?
column y?
(A) int nC = [Link];
(A) array[x[y]] (B) int nC = a[0].length;
(B) array[y][x] (C) int nC = [Link]();
(C) array[x][y] (D) int nC = a[0].size();
(D) [Link](x,y)

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
Answer questions 7 and 8 based on the 9) Which of the following is a valid
code below. way of traversing a two-dimensional
array from the top left to the bottom
right?

I.

7) Which of the following is


accomplished by the code above? II.

(A) The elements of the two-


dimensional array are printed,
starting at the top left and ending
in the bottom right
(B) The elements of the two- III.
dimensional array are printed,
starting at the bottom left and ‘
ending in the top right
(C) The elements of the two-
dimensional array are printed,
starting at the top right and (A) I only
ending in the bottom left (B) II only
(D) The elements of the two- (C) III only
dimensional array are printed, (D) II and III
starting at the bottom right and (E) I, II, and III
ending in the top left
10) Which initialization of the array
8) What output will result from the variable would result in a integer
code above? two-dimensional array the same
dimensions as the one in the code
(A) All of the output lines will be 1. below?
(B) All of the output lines will be
nil.
(C) All of the output lines will be 0.
(D) All of the output lines will be 4. (A) array = new int[3][6];
(E) There would be an error because (B) array = new int[2][3];
the values inside the two- (C) array = new int[3][2];
dimensional array were never set. (D) array = new int[6][3];

END OF SECTION I
This practice test was created by Ajay Gandecha.
This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
Section II – Free Response Section
Optional Time – 15 minutes
1 Question

1. This question involves the representation of a battlefield by the following Battlefield


class.

public class Battlefield


{
/** Data fields. */
private boolean[][] battlefieldGrid;

/** Returns true if a location on the battlefield has a land mine, returns false
* otherwise.
*/
public boolean hasMine(int row, int col)
{ /* to be implemented in part (a) */ }

/** Returns true if a soldier can walk across the battlefield at a given row
* without hitting a land mine, returns false otherwise.
*/
public boolean canSafelyCross(int rowToCross)
{ /* to be implemented in part (b) */ }

// There may be instance variables, constructors, and methods not shown.

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
(a) Write the Battlefield method hasMine. This method will return true if there is a
land mine at the row and column position in the battlefieldGrid field, and will
return false otherwise.

The following is a representation of the battlefieldGrid array:

true – Land mine exists in area


false – Land mine does not exist in area

X 0 1 2 3 4

0 true true false false false

1 false true false false false

2 false false false false false

3 false false false true false

4 false true false false false

Class information for this question

public class Battlefield

private int[][] battlefieldGrid;

public boolean hasMine(int row, int col);


public boolean canSafelyCross(int row);

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
Complete the hasMine method below.

/** Returns true if a location on the battlefield has a land mine, returns false
* otherwise.
*/
public boolean hasMine(int row, int col)

public boolean hasMine (int row, int col) {


boolean mineAtLoc = [Link] [row][col];
return mineAtLoc;
}

NOTE THAT THE BATTLEFIELD GRID ALREADY HAS THE BOOLEAN VALUES

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
(b) Write the Battlefield method canSafelyCross. This method will return true if
a soldier can safely cross the battlefield, represented by the battlefieldGrid field,
without hitting any mines, and will return false otherwise.

NOTE: You must use the hasMine method appropriately to receive full credit.

For example:

X 0 1 2 3 4

0 true true false false false

1 false true false false false

2 false false false false false

3 false false false true false

4 false true false false false

In the example, row 2 is the only one that would return true, as it is the only row
without any land mines.

Class information for this question

public class Battlefield

private int[][] battlefieldGrid;

public boolean hasMine(int row, int col);


public boolean canSafelyCross(int row);

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
Complete the canSafelyCross method below.

/** Returns true if a soldier can walk across the battlefield at a given row
* without hitting a land mine, returns false otherwise.
*/
public boolean canSafelyCross(int rowToCross)

public boolean canSafelyCross (int rowToCross) {


boolean canSafelyCross = true
for (int i = 0; i < [Link][0].length; i++) {
if (hasMine ([rowToCross] [i]) == true)
canSafelyCross = false;
}
return canSafelyCross;

}
}

END OF SECTION II
This practice test was created by Ajay Gandecha.
This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!

Common questions

Powered by AI

The third statement is incorrect because, in Java, the indexing of arrays, including two-dimensional arrays, starts at zero, not one. Thus, the first row of an array is located at the index of 0, not 1 .

It is important to declare access modifiers, such as 'private', for two-dimensional array fields storing sensitive data to ensure encapsulation. This prevents unauthorized access and modification from external classes, maintaining the integrity and security of the data. By controlling access, the class protects sensitive information from exposure and misuse .

In Java, when a two-dimensional array of integers is created using the 'new' keyword, all the integer elements are initialized to zero by default. This behavior is part of Java's default initialization process where array elements are set to their default values, which for integers is zero .

The Battlefield class uses a method that iterates through each column in the specified row and utilizes the 'hasMine' method to check for mines. If any mine is found, 'canSafelyCross' returns false; otherwise, it returns true. This method is effective because it leverages an existing helper method to minimize code duplication and ensures only safe rows, free from mines, allow safe passage .

The 'hasMine' method checks the specified coordinates in the 'battlefieldGrid' array to determine the presence of a landmine. If the element at the given row and column is 'true', it indicates there is a landmine, otherwise, there is none. This direct checking of array indices ensures accurate and fast determination based on stored boolean values .

If the values in a two-dimensional array are not set before their use, this can lead to runtime errors or undefined behavior, such as 'null pointer exceptions' for objects or default values that might not reflect the intended logic, as these cells may contain default values or remain unset .

Initializing an array with 'new int[3][6]' would be appropriate if you're setting up a structure requiring exactly three rows and six columns to represent a specific model or dataset whose structure naturally fits this configuration, such as a grid for a game board or a matrix for mathematical computations where these dimensions correspond to logical partitions of data .

In a two-dimensional array, all elements must be of the same or related types to ensure type safety and consistency in operations that are performed on the array elements. This ensures that operations such as arithmetic or logic operations are valid for all elements without type errors. Using elements of different or unrelated types could lead to unpredictable behavior or runtime errors when accessing or modifying array elements .

To access a specific element in a two-dimensional array at a given row and column index, the correct syntax is 'array[row][column]'. This uses the standard row-major order to address elements in a matrix-like data structure .

In row-major order, elements of each row of the array are contiguous in memory. This means that the array is stored with all elements of the first row, followed by all elements of the second row, and so on. In column-major order, elements of each column are contiguous, with all elements of the first column, followed by the second column, and so forth. This orientation affects performance, especially in languages or systems where this order influences cache utilization during operations over the array .

You might also like