2D Array Creation and Access

Help Questions

AP Computer Science A › 2D Array Creation and Access

Questions 1 - 7
1

Based on the scenario, what does inventory20 represent in a warehouse inventory 2D array?

A single 1D array for category 2.

Total stock across all categories.

Category 2 stock level in column 0.

Category 0 stock level in column 2.

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. In a 2D array representing warehouse inventory, the first index typically represents the category or item type, while the second index represents a specific attribute like location or time period. The notation inventory[2][0] accesses the element at row 2 (third category, due to zero-based indexing) and column 0 (first column). Choice A is correct because inventory[2][0] represents the stock level for category 2 in column 0, following the standard row-column interpretation. Choice B is incorrect because it misinterprets the indices, suggesting category 0 in column 2 rather than category 2 in column 0. To help students: Use real-world inventory examples with clear row and column labels, practice interpreting what each index represents in context, and reinforce the importance of understanding the data structure's design. Watch for: confusion about which index represents which dimension and misunderstanding zero-based indexing.

2

In the context of the passage, how do you initialize the 2D array grades for 3 tests by 4 students?​

int[][] grades = new int[3][4];

int[][] grades = new int[3][];

int[] grades = new int[3][4];

int[][] grades = new int[4][3];

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. A 2D array declaration in Java requires specifying the data type followed by two sets of square brackets, and initialization uses the new keyword with dimensions. The syntax new int[rows][columns] creates an array with the specified number of rows and columns. Choice A is correct because int[][] grades = new int[3][4] properly creates a 2D integer array with 3 rows (for 3 tests) and 4 columns (for 4 students). Choice B is incorrect because new int[4][3] reverses the dimensions, creating 4 rows and 3 columns instead. To help students: Use real-world analogies like spreadsheets where rows represent one dimension and columns another, practice writing array declarations for various scenarios, and reinforce the row-first convention. Watch for: confusion between rows and columns, and forgetting the double brackets in the declaration.

3

Based on the scenario, what is the value stored at board02 after X moves there on a tic-tac-toe board?

It stores ' '.

It stores 'T'.

It stores 'X'.

It stores 'O'.

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. A 2D array stores values at specific positions identified by row and column indices, both starting from 0. When a value is assigned to an array position, it replaces any previous value at that location. Choice B is correct because after X moves to board[0][2], that position stores the character 'X' as indicated in the scenario. Choice C is incorrect because it suggests the position remains empty (stores a space character), which would be the case before the move but not after. To help students: Use tic-tac-toe as a concrete example to visualize 2D array operations, practice tracing through game moves and their corresponding array updates, and emphasize that array assignments overwrite previous values. Watch for: students forgetting that assignments change the array's state and confusion about initial versus current values.

4

In the context of the passage, which line of code accesses revenue at row 2, column 1 in finance?

double v = finance[1][2];

double v = finance[2][1];

double v = finance[2][0];

double v = finance[3][1];

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. A 2D array for financial data typically organizes information with rows representing one dimension (like time periods or categories) and columns representing another (like different metrics). Accessing a specific value requires using the correct row and column indices in order. Choice B is correct because finance[2][1] properly accesses the revenue value at row 2, column 1 as specified in the question. Choice A is incorrect because finance[1][2] would access row 1, column 2, reversing the required indices. To help students: Create sample financial tables with clear row/column labels, practice mapping between descriptive locations and array indices, and emphasize the consistent row-first indexing pattern. Watch for: students reversing row and column indices and forgetting that array indexing starts at 0.

5

Based on the scenario, what is the value stored at board22 after O moves there on tic-tac-toe?

It stores ' '.

It stores '2'.

It stores 'X'.

It stores 'O'.

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. In a tic-tac-toe game represented as a 2D array, each position can store a character representing the player's mark ('X' or 'O') or an empty space. When a player makes a move, their mark is stored at the specified array position. Choice B is correct because after O moves to board[2][2], that position stores the character 'O' as indicated in the scenario. Choice A is incorrect because it suggests 'X' is stored there, which would only be true if player X had moved to that position instead. To help students: Use game boards as concrete examples for 2D arrays, trace through game sequences showing how the array changes with each move, and emphasize that each position stores the mark of the player who moved there. Watch for: confusion about which player's mark is stored and misunderstanding the game state.

6

In the context of the passage, which line of code accesses stock at row 1, column 3 in inventory?

int s = inventory[1][4];

int s = inventory[1][3];

int s = inventory[1];

int s = inventory[3][1];

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. A 2D array uses the syntax array[row][column] to access elements, with both indices starting from 0. For an inventory system, rows might represent different items while columns represent different locations or time periods. Choice B is correct because inventory[1][3] properly accesses the stock value at row 1, column 3 as required by the question. Choice A is incorrect because inventory[3][1] would access row 3, column 1, which reverses the specified indices. To help students: Create visual representations of inventory grids with clear labels, practice converting between descriptive positions and array indices, and consistently reinforce the row-column order. Watch for: students reversing the order of indices and confusion about zero-based indexing.

7

Based on the scenario, which line of code accesses the student at row 1, column 2 in seats?

String name = seats[2][1];

String name = seats[1][3];

String name = seats[1][2];

String name = seats[1];

Explanation

This question tests AP Computer Science A skills, specifically understanding 2D array creation and access in data collections. A 2D array is a collection of elements arranged in a grid, accessible via two indices: one for the row and one for the column. In Java, 2D arrays use the syntax array[row][column] where both indices start at 0. Choice B is correct because seats[1][2] properly accesses the element at row 1, column 2 as specified in the question. Choice A is incorrect because seats[2][1] would access row 2, column 1, reversing the required indices. To help students: Draw visual representations of 2D arrays with labeled rows and columns, practice tracing through array accesses step-by-step, and emphasize that the first index is always the row. Watch for: students confusing row/column order and forgetting that array indices start at 0.