Fiveable
💻AP Computer Science A
​

💻AP Computer Science A

FRQ 4 – 2D Array
​
Unit 4: Data Collections
AP CS A FRQ Types & Units

Each FRQ type tests specific skills taught in particular units. Here's why certain units appear for each question type:

FRQFocusUnitsWhy
FRQ 1Methods and Control Structures1-2Tests method writing, loops, conditionals - foundational programming from Units 1-2
FRQ 2Class Design3Tests class design, constructors, instance variables - all Unit 3 OOP content
FRQ 3Data Analysis with ArrayList4Tests using, analyzing, and manipulating data in an ArrayList - Unit 4 content
FRQ 42D Array4Tests 2D array traversal and manipulation - Unit 4 content

This mapping reflects College Board's exam structure - each FRQ type tests specific skills that are taught in particular units.

Practice FRQ 1 of 201/20

4. The VendingMachine class represents a grid of items stocked in a machine. A partial declaration of the VendingMachine class is shown below.

</>Java
public class Item
{
    /**
     * Returns the category of the item (e.g., "Snack", "Drink", "Candy")
     */
    public String getCategory()
    { /* implementation not shown */ }

    /**
     * Returns the price of the item
     */
    public double getPrice()
    { /* implementation not shown */ }

    /* There may be instance variables, constructors, and methods
       that are not shown. */
}
</>Java
public class VendingMachine
{
    private Item[][] slots;

    /**
     * Returns the index of the column containing the greatest number of
     * items belonging to the specified category.
     * If multiple columns have the same greatest number, the lowest of
     * those column indices is returned.
     * Preconditions: slots is not null and no elements of slots are null.
     *                slots has at least one row and at least one column.
     */
    public int columnWithMost(String category)
    { /* to be implemented */}

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

When an element of the two-dimensional array slots is accessed, the first index is used to specify the row and the second index is used to specify the column.

Write the VendingMachine method columnWithMost. The method should return the index of the column in slots that contains the greatest number of items where the category matches the parameter category. If there are multiple columns that are tied for the greatest number of matching items, the lowest index among those columns should be returned.

Suppose slots has the following contents. Each cell displays the category of the Item at that location.

Row

0

1

2

3

0

"Drink"

"Snack"

"Drink"

"Candy"

1

"Drink"

"Snack"

"Snack"

"Candy"

2

"Candy"

"Snack"

"Drink"

"Candy"

  • Call: columnWithMost("Snack")

  • Expected Return: should return 1

  • Explanation: because column 1 contains 3 "Snack" items, which is more than any other column.

  • Call: columnWithMost("Drink")

  • Expected Return: should return 0

  • Explanation: because column 0 and column 2 both contain 2 "Drink" items. Since 2 is the greatest number found and 0 is the lower index, 0 is returned.

  • Call: columnWithMost("Candy")

  • Expected Return: should return 3

  • Explanation: because column 3 contains 3 "Candy" items, which is the maximum for this category.

Complete method columnWithMost.

</>Java
/**
 * Returns the index of the column containing the greatest number of
 * items belonging to the specified category.
 * If multiple columns have the same greatest number, the lowest of
 * those column indices is returned.
 * Preconditions: slots is not null and no elements of slots are null.
 *                slots has at least one row and at least one column.
 */
public int columnWithMost(String category)
Timed

00:00

Response auto-saves






Pep

essential ap study content awaits..

Features
Testimonials
Testimonials
start studying →
FRQ Directions
Free Response Question Practice

This practice environment simulates the AP AP Computer Science A Free Response Questions section. Here are some guidelines:

  • Read each question carefullybefore responding. Pay attention to command verbs like "identify," "explain," "analyze," or "evaluate."
  • Use the timer to practice time management. You can pause, restart, or hide the timer as needed.
  • Mark for Review if you want to come back to a question later.
  • Your responses are saved automatically as you type. You can also use the drawing tool for questions that require diagrams or graphs.
  • Use the toolbar for formatting options like bold, italic, subscript, and superscript.
  • Navigate between questions using the Previous and Next buttons at the bottom of the screen.

Tip: Answer all parts of each question. Partial credit is often available, so even if you are unsure, provide what you know.