ACSA Exam Prep
← All free-response questions
Question 4 · 2D Array · 6 points

Classroom Seating

A teacher's seating chart is a 2D array of student names. Row 0 is the front of the room. An empty seat holds null.

public class SeatingChart
{
    /** The seating chart; rectangular, at least 1 x 1.
     *  Empty seats are null.
     */
    private String[][] seats;

    /** Moves students forward as described below and returns
     *  the number of students whose seat changed.
     */
    public int moveForward()
    { /* to be implemented */ }

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

Your class

6 points

Write moveForward. In each column, students move toward row 0 to fill empty seats. Students in a column keep their front-to-back order, and all empty seats end up at the back of the column. Return the number of students who ended up in a different seat.

Before (. means empty):

col 0col 1col 2
row 0.Bo.
row 1Ana..
row 2.CyDi
row 3Ed.Fay

After moveForward(), which returns 5:

col 0col 1col 2
row 0AnaBoDi
row 1EdCyFay
row 2...
row 3...
Tab inserts spaces. Drafts save as you type.
Self-score
0 / 6