Skip to main content

Question 4: 2D Array

Filling a letter grid from a String

  • Units 1 and 4
  • 6 points
  • About 22 minutes

You write one method that traverses, analyzes or changes data in a 2D array, often of objects, using the classes described in the question. New since the course was revised for 2025–26 (first tested in May 2026): it's worth 6 points instead of 9, and points are no longer taken off as penalties. On the exam: Question 4 of 4, 6 points. Section II has 4 free-response questions in 90 minutes (45% of the score), all assessing Practice 2 (Develop Code). The exam is fully digital in Bluebook: you type your Java, and the Java Quick Reference is provided. No calculator. Suggested time is our split of the 90 minutes by points; College Board gives no per-question timing.

The question and its sources

A word-puzzle game places letters in a grid. The LetterGrid class stores the grid as a two-dimensional array of one-letter Strings. When an element of a two-dimensional array is accessed, the first index is the row and the second index is the column.

The LetterGrid class

Java
public class LetterGrid
{
    /** The letters in the grid, each a String of length 1 */
    private String[][] grid;
 
    /**
     * Creates a grid with numRows rows and numCols columns and fills it
     * from letters, as described in the question.
     * Preconditions: numRows > 0, numCols > 0
     *                letters contains only uppercase letters
     */
    public LetterGrid(String letters, int numRows, int numCols)
    { /* to be implemented */ }
 
    /** Returns the letter at row r, column c */
    public String getLetter(int r, int c)
    { return grid[r][c]; }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example: the grid made by new LetterGrid("PLANETS", 3, 3)

RowColumn 0Column 1Column 2
0"P""L""A"
1"N""E""T"
2"S""*""*"

Source: Hypothetical example

Example: the grid made by new LetterGrid("ABCDEFGH", 2, 3)

RowColumn 0Column 1Column 2
0"A""B""C"
1"D""E""F"

Source: Hypothetical example

Suggested time: 22 minutes

Your answers are saved in this browser as you type.

Something wrong with this question?

What's wrong?

Please don't include personal details.

Part (a)

6 points

Write the LetterGrid constructor. It creates grid with numRows rows and numCols columns and fills it in *row-major order*: all of row 0 from left to right, then all of row 1, and so on. The first letter of letters goes in row 0, column 0, the next letter in row 0, column 1, and so on, one letter per element. If letters runs out before the grid is full, every remaining element is "*". If letters has more letters than the grid has elements, the extra letters are not used. Complete the LetterGrid constructor.public LetterGrid(String letters, int numRows, int numCols)

Tab indents. Press Esc, then Tab, to move on.

0 lines · 0 / 2,500 characters

Checking scoring…

Scoring it yourself shows you the rubric, examples and a model answer. Try writing your answer first.