2020 Practice Exam FRQ

Method and Control Structure FRQ

  • Write 1–2 methods using loops + conditionals.
  • Input: Often arrays or ArrayLists.
  • Goal: Process data, return result (count, sum, modified list, etc).
  • Tips:
    • Read carefully, focus on exactly what’s asked.
    • Keep logic simple and clear.
    • Use helper methods if needed.
public static int hailstoneLength(int n) {
    int length = 1;

    while (n != 1) {
        if (n % 2 == 0) {
            n = n / 2; 
        } else {
            n = 3 * n + 1; 
        }
        length++; 
    }

    return length;
}
hailstoneLength(3);
8
public static boolean isLongSeq(int n){
        int length = hailstoneLength(n);
        if (length > n) {
            return true;
        }
        return false;
}
System.out.println(isLongSeq(8));
false
public static double propLongList(int limit) {
    int longSequenceCount = 0;

    for (int startValue = 1; startValue <= limit; startValue++) {
        int length = hailstoneLength(startValue);
        if (length > startValue) {
            longSequenceCount++;
        }
    }

    if (limit > 0) {
        return (double) longSequenceCount / limit;
    } else {
        return 0.0; 
    }
}
propLongList(10) // so basically this method allows us to return like the percentage/proprotion of the long hailstone lists
0.5

Classes FRQ

  • Focus: Writing a class with fields, constructors, and methods.
  • You’ll likely:
    • Declare private instance variables
    • Write a constructor to initialize them
    • Implement accessor (get) and mutator (set) methods
    • Write custom methods that use the instance variables
  • Tips:
    • Write method stubs first, then fill in logic
    • Keep variable and method names clear and descriptive
    • Check that each method does exactly what the prompt asks
public class GameSpinner {
    private int sectors;
    private int lastSpin;
    private int currentRun;
    private boolean firstSpin;
    
    public GameSpinner(int sectors) {
        this.sectors = sectors;
        this.currentRun = 0;
        this.firstSpin = true;
    }
    
    public int spin() {
        int result = (int)(Math.random() * sectors) + 1;
        
        if (firstSpin) {
            lastSpin = result;
            currentRun = 1;
            firstSpin = false;
        } else if (result == lastSpin) {
            currentRun++;
        } else {
            lastSpin = result;
            currentRun = 1;
        }
        
        return result;
    }
    
    public int currentRun() {
        return currentRun;
    }
}
GameSpinner g = new GameSpinner(4);
System.out.println("Spin result: " + g.spin());
System.out.println("Current run: " + g.currentRun());

System.out.println("Spin result: " + g.spin());
System.out.println("Current run: " + g.currentRun());

System.out.println("Spin result: " + g.spin());
System.out.println("Current run: " + g.currentRun());
Spin result: 2
Current run: 1
Spin result: 3
Current run: 1
Spin result: 2
Current run: 1

3-ArrayLists

  • Focus: Traversing and modifying ArrayList objects.
  • You’ll usually:
    • Loop through an ArrayList using for or while
    • Add, remove, or update elements
    • Return counts, modified lists, or boolean results
  • Key methods to know:
    • add(index, value)
    • get(index)
    • set(index, value)
    • remove(index)
    • size()

public void addReview(ProductReview prodReview) {
    reviewList.add(prodReview);
    String productName = prodReview.getName();
    
    boolean productExists = false;
    for (String product : productList) {
        if (product.equals(productName)) {
            productExists = true;
            break;
        }
    }
    if (!productExists) {
        productList.add(productName);
    }
}
public int getNumGoodReviews(String prodName) {
    int count = 0;
    for (ProductReview review : reviewList) {
        if (review.getName().equals(prodName)) {
            String reviewText = review.getReview().toLowerCase();
            if (reviewText.contains("best")) {
                count++;
            }
        }
    }
    
    return count;
}

2D Array FRQ – Quick Notes

  • Focus: Accessing and modifying 2D arrays (int[][], String[][], etc).
  • You’ll usually:
    • Traverse using nested for loops
    • Count, sum, or find values
    • Modify elements based on conditions

BE CAREFUL OF END BOUNDS

public Theater(int seatsPerRow, int tier1Rows, int tier2Rows) {
    theaterSeats = new Seat[tier1Rows + tier2Rows][seatsPerRow];

    for (int i = 0; i < tier1Rows; i++) {
        for (int j = 0; j < seatsPerRow; j++) {
            theaterSeats[i][j] = new Seat(true, 1);
        }
    }

    for (int i = tier1Rows; i < tier1Rows + tier2Rows; i++) {
        for (int j = 0; j < seatsPerRow; j++) {
            theaterSeats[i][j] = new Seat(true, 2);
        }
    }
}
public boolean reassignSeat(int fromRow, int fromCol, int toRow, int toCol) {
    if (fromRow < 0 || fromRow >= theaterSeats.length || fromCol < 0 || fromCol >= theaterSeats[fromRow].length ||
        toRow < 0 || toRow >= theaterSeats.length || toCol < 0 || toCol >= theaterSeats[toRow].length) {
        return false;
    }

    Seat sourceSeat = theaterSeats[fromRow][fromCol];
    Seat destinationSeat = theaterSeats[toRow][toCol];

    if (sourceSeat.isAvailable()) {
        return false;
    }

    if (!destinationSeat.isAvailable()) {
        return false;
    }

    int sourceTier = sourceSeat.getTier();
    int destinationTier = destinationSeat.getTier();

    if (sourceTier == 1) {
        if (destinationTier < sourceTier) {
            return false;
        }
    } else if (sourceTier == 2) {
        if (destinationTier != sourceTier) {
            return false;
        }
    }

    sourceSeat.setAvailability(true);
    destinationSeat.setAvailability(false);
    return true;
}