AP CSA Exam Format

Section I: Multiple Choice

40 Questions | 1 Hour 30 Minutes | 50% of Exam Score

The multiple-choice section includes mostly individual questions, occasionally with 1–2 sets of questions (2 questions per set). Computational Thinking Practices 1, 2, 4, and 5 are all assessed in the multiple-choice section.

Section II: Free Response

4 Questions | 1 Hour 30 Minutes | 50% of Exam Score

All free-response questions assess Computational Thinking Practice 3: Code Implementation, with the following focus:

Question 4 - 2D Array

PART A

moveCandyToFirstRow is a method that moves a piece of candy to the first row of a 2D box (grid/table with rows and columns) of candies. *Write the purpose:

This is an image

// method that belongs to BoxOfCandy class, move candy to first row of 2d box (2d array)
// takes parameter specified column number col
public boolean moveCandyToFirstRow(int col)
{
    // check if element at row 0 (first row) and specified col already has candy - CB CRITERIA
    if(box[0][col] != null)
    {
        // if there is candy, no need to move anything
        return true;
    }
    
    // iterate through remaining rows and cols - CB CRITERIA
    // start from row 1 (second row)
    for(int row = 1; row < box.length; row++)
    {
        // check if current row and col has candy - CB CRITERIA
        if(box[row][col] != null)
        {
            // if there is candy
            // move candy to row 0 and specified col - CB CRITERIA 
            box[0][col] = box[row][col];
            // set prev location of candy to null (remove it from og position) - CB CRITERIA
            box[row][col] = null;
            // return true to indicate move success
            return true;
        }
    }
    // no candy found in specified col
    // return false to indicate move unsuccess
    return false;
}

Here are some key learning points:

Collegeboard Criteria and Grading: 4/4

PART B

removeNextByFlavor is a method that removes and returns the next piece of candy with a specific flavor from a 2D box (grid/table with rows and columns) of candies.

This is an image

// method that belongs to BoxOfCandy class, remove candy with specified flavor
// takes parameter specified flavor
public Candy removeNextByFlavor(String flavor)
{
    // nested loops to traverse candy box starting from last row, moving up
    // traverse - visit each element in array - CB CRITERIA 
    for(int row = box.length - 1; row >= 0; row--)
    {
        for(int col = 0; col < box[0].length; col++)
        {
            // check if current box element has candy AND if flavor matches specified flavor- CB CRITERIA 
            // call getFlavor on Candy object to check flavor - CB CRITERIA 
            if(box[row][col] != null &&
                    box[row][col].getFlavor().equals(flavor)) 
            {
                // store selected candy
                Candy selected = box[row][col];
                // remove candy from box by set box element to null  - CB CRITERIA 
                box[row][col] = null;
                // return the removed selected candy
                return selected;
            }
        }
    }
    // no candy with specified flavor found
    // return false to indicate flavor search unsuccess
    return null;
}

Here are some key learning points:

Collegeboard Criteria and Grading: 5/5

import java.util.Scanner;

// This question involves pieces of candy in a box. The Candy class represents a single piece of candy.
class Candy {
    String flavor; // class member
    
    // constructor for default flavor (for when no input flavor yet)
    public Candy() {
        flavor = "empty";
    }
    
    // constructor for input flavor
    public Candy(String inputflavor) {
        flavor = inputflavor;
    }
    
    // initialize flavor with specified input flavor
    void setFlavor(String inputflavor) {
        flavor = inputflavor;
    }
    public String getFlavor() {
        return flavor;
    }
}

public class BoxOfCandy {
    private Candy[][] box;
    Scanner input = new Scanner(System.in); // scanner for user input

    // allocate/assign memory
    public BoxOfCandy(int row, int col) {
        box = new Candy[row][];
        for (int j = 0; j<row; j++) {
            box[j] = new Candy[col];
        }
    }
    
    // fill random places in box with random candy flavors
    public void fillBoxWithRandomCandy() {
        String[] possibleFlavors = {"Orange", "Lime", "Lemon"};
        Random random = new Random();
    
        for (int row = 0; row < box.length; row++) {
            for (int col = 0; col < box[0].length; col++) {
                // probability for how many boxes get filled (0.3 is 30% chance filled)
                if (random.nextDouble() < 0.3) { 
                    int randomIndex = random.nextInt(possibleFlavors.length);
                    String randomFlavor = possibleFlavors[randomIndex];
                    box[row][col] = new Candy(randomFlavor);
                }
            }
        }
    }
    
    // PART A
    // method, move candy to first row of 2d box (2d array)
    public boolean moveCandyToFirstRow(int col) {
        // check if element at row 0 (first row) and specified col already has candy
        if (box[0][col] != null) {
            // if there is candy, no need to move anything
            return true;
        }

        // iterate through remaining rows and cols
        // start from row 1 (second row)
        for (int row = 1; row < box.length; row++) {
            // check if current row and col has candy
            if (box[row][col] != null) {
                // if there is candy
                // move candy to row 0 and specified col
                box[0][col] = box[row][col];
                // set prev location of candy to null (remove it from og position)
                box[row][col] = null;
                // return true to indicate move success
                return true;
            }
        }
        // no candy found in specified col
        // return false to indicate move unsuccess
        return false;
    }

    // PART B
    // method that removes specific candy by flavor
    public Candy removeNextByFlavor(String flavor) {
        // traverse candy box starting from last row, moving up
        // traverse - visit each element in array
        for (int row = box.length - 1; row >= 0; row--) {
            for (int col = 0; col < box[0].length; col++) {
                // check if current box element has candy AND if flavor matches specified flavor
                if (box[row][col] != null &&
                        box[row][col].getFlavor().equals(flavor)) {
                    // store selected candy
                    Candy selected = box[row][col];
                    // remove candy from box by set box element to null
                    box[row][col] = null;
                    // return the removed selected candy
                    return selected;
                }
            }
        }
        // no candy with specified flavor found
        // return false to indicate flavor search unsuccess
        return null;
    }
    // Main method for testing
    public static void main(String[] args) {
        BoxOfCandy candyBox = new BoxOfCandy(5, 5); // initiating object candyBox, 5 by 5 box of candy

        // fill box with candy flavors
        candyBox.fillBoxWithRandomCandy();
        
        // Get user input for specified column and move candy to the first row
        // make sure input is valid so no bound errors - *TODO
        System.out.print("Enter the column index(0-4): ");
        int col = candyBox.input.nextInt();
        boolean moveSuccess = candyBox.moveCandyToFirstRow(col);
        System.out.println(col);

        if (moveSuccess) {
            System.out.println("Candy moved successfully to the first row.");
        } else {
            System.out.println("No candy found in the specified column.");
        }

        // PART B
        // Get user input and remove candy by flavor
        System.out.print("\nEnter the flavor of candy to remove: ");
        String flavor = candyBox.input.next();
        Candy removeSuccess = candyBox.removeNextByFlavor(flavor);
        System.out.println(flavor);

        if (removeSuccess!=null) {
            System.out.println("Removed candy with flavor sucessfully.");
        } else {
            System.out.println("No candy with the specified flavor found.");
        }
    }
}
BoxOfCandy.main(null)
Enter the column index(0-4): 2
Candy moved successfully to the first row.

Enter the flavor of candy to remove: Orange
Removed candy with flavor sucessfully.

*Did extra for running code:

*TODO - Things to improve with overall program: