Skip to main content

Question 4: 2D Array

Mirroring a picture left to right

  • Unit 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 drawing app stores a black-and-white picture as a two-dimensional array of brightness values from 0 (black) to 255 (white). The Picture class holds the array. When an element of a two-dimensional array is accessed, the first index is the row and the second index is the column.

The Picture class

Java
public class Picture
{
    /**
     * The brightness of each pixel. pixels has at least one row and at
     * least one column, and every row has the same length.
     */
    private int[][] pixels;
 
    /**
     * Flips the picture left to right, as described in the question.
     */
    public void mirror()
    { /* to be implemented */ }
 
    // There may be instance variables, constructors, and methods that are not shown.
}

Example: pixels before the call mirror()

RowColumn 0Column 1Column 2Column 3Column 4
01020304050
100255255255
278980

Source: Hypothetical example

The same pixels after the call

RowColumn 0Column 1Column 2Column 3Column 4
05040302010
125525525500
208987

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 Picture method mirror, which changes pixels so the picture is flipped left to right: in every row, the value that was in column 0 ends up in the last column, the value in column 1 ends up in the second-to-last column, and so on. The rows stay in the same order. The method must work for pictures with an odd or an even number of columns. Complete method mirror.public void mirror()

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.