2D Array Creation and Access

Help Questions

AP Computer Science A › 2D Array Creation and Access

Questions 1 - 4
1

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

A single 1D array for category 2.

Category 0 stock level in column 2.

Category 2 stock level in column 0.

Total stock across all categories.

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

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

It stores 'O'.

It stores 'X'.

It stores ' '.

It stores '2'.

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.

3

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

It stores 'O'.

It stores ' '.

It stores 'X'.

It stores 'T'.

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

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

String name = seats[1][2];

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.