Sprint View

2015 FRQ 1

9 min read

AP Computer Science A

2015 Free-Response Questions — Study Version


Question 1: Arrays & 2D Arrays (DiverseArray)

Key Lessons

  • Traversing 1D arrays
  • Traversing 2D arrays by rows
  • Using helper methods (very important on AP!)
  • Checking for duplicate values

Part (a): arraySum

Lesson: Loop through a 1D array and accumulate a total.

Code Runner Challenge

DIVERSEARRAY

View IPYNB Source
// CODE_RUNNER: DIVERSEARRAY

public class Main {

    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int value : arr) {
            sum += value;
        }
        return sum;
    }

    public static void main(String[] args) {
        int[] arr = {1, 3, 2, 7, 3};
        System.out.println(arraySum(arr));
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part (b): rowSums

Lesson: Each row of a 2D array is itself a 1D array. You are required to reuse arraySum.

Code Runner Challenge

ROWSUMS

View IPYNB Source
// CODE_RUNNER: ROWSUMS

public class Main {

    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int value : arr) {
            sum += value;
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D) {
        int[] sums = new int[arr2D.length];

        for (int i = 0; i < arr2D.length; i++) {
            sums[i] = arraySum(arr2D[i]);
        }
        return sums;
    }

    public static void main(String[] args) {
        int[][] mat = {
            {1, 3, 2, 7, 3},
            {10, 10, 4, 6, 2}
        };

        int[] result = rowSums(mat);
        for (int x : result) {
            System.out.print(x + " ");
        }
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part (c): isDiverse

Lesson:

  • Use rowSums
  • Check that no two row sums are equal
  • Nested loops are expected

Code Runner Challenge

ISDIVERSE

View IPYNB Source
// CODE_RUNNER: ISDIVERSE

public class Main {

    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int value : arr) {
            sum += value;
        }
        return sum;
    }

    public static int[] rowSums(int[][] arr2D) {
        int[] sums = new int[arr2D.length];
        for (int i = 0; i < arr2D.length; i++) {
            sums[i] = arraySum(arr2D[i]);
        }
        return sums;
    }

    public static boolean isDiverse(int[][] arr2D) {
        int[] sums = rowSums(arr2D);

        for (int i = 0; i < sums.length; i++) {
            for (int j = i + 1; j < sums.length; j++) {
                if (sums[i] == sums[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int[][] mat = {
            {1, 3, 2, 7, 3},
            {10, 10, 4, 6, 2}
        };

        System.out.println(isDiverse(mat));
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Question 2: Strings & Classes (HiddenWord)

Key Lessons

  • String traversal
  • charAt
  • String concatenation
  • Matching characters with conditions

HiddenWord Class

Code Runner Challenge

HIDDENWORD

View IPYNB Source
// CODE_RUNNER: HIDDENWORD

public class Main {

    static class HiddenWord {
        private String hidden;

        public HiddenWord(String word) {
            hidden = word;
        }

        public String getHint(String guess) {
            String hint = "";

            for (int i = 0; i < guess.length(); i++) {
                char g = guess.charAt(i);

                if (g == hidden.charAt(i)) {
                    hint += g;
                } else if (hidden.indexOf(g) >= 0) {
                    hint += "+";
                } else {
                    hint += "*";
                }
            }
            return hint;
        }
    }

    public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("HARPS");
        System.out.println(puzzle.getHint("HEART")); // H*++*
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Exam Insight: indexOf(char) is the cleanest way to check if a letter exists elsewhere.


Question 3: Sparse Arrays

Key Lessons

  • Using ArrayList
  • Searching objects
  • Removing while iterating (loop backward!)
  • Updating indices

Part (a): getValueAt

Code Runner Challenge

GETVALUEAT

View IPYNB Source
// CODE_RUNNER: GETVALUEAT

import java.util.*;

public class Main {

    static class SparseArrayEntry {
        private int row, col, value;

        public SparseArrayEntry(int r, int c, int v) {
            row = r;
            col = c;
            value = v;
        }

        public int getRow() { return row; }
        public int getCol() { return col; }
        public int getValue() { return value; }
    }

    static class SparseArray {
        private List<SparseArrayEntry> entries = new ArrayList<>();

        public int getValueAt(int row, int col) {
            for (SparseArrayEntry e : entries) {
                if (e.getRow() == row && e.getCol() == col) {
                    return e.getValue();
                }
            }
            return 0;
        }
    }

    public static void main(String[] args) {
        SparseArrayEntry entry = new SparseArrayEntry(2, 2, 5);

        SparseArray array = new SparseArray();

        System.out.println(array.getValueAt(1, 2));
    }
}

Main.main(null);

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part (b): removeColumn

Critical AP Rule: When removing from a list → loop backwards.

Code Runner Challenge

REMOVECOLUMN

View IPYNB Source
// CODE_RUNNER: REMOVECOLUMN

import java.util.*;

public class Main {

    static class SparseArrayEntry {
        private int row, col, value;

        public SparseArrayEntry(int r, int c, int v) {
            row = r;
            col = c;
            value = v;
        }

        public int getRow() { return row; }
        public int getCol() { return col; }
        public int getValue() { return value; }
    }

    static class SparseArray {
        private int numCols;
        private List<SparseArrayEntry> entries = new ArrayList<>();

        public void removeColumn(int col) {
            for (int i = entries.size() - 1; i >= 0; i--) {
                SparseArrayEntry e = entries.get(i);

                if (e.getCol() == col) {
                    entries.remove(i);
                } else if (e.getCol() > col) {
                    entries.set(i,
                        new SparseArrayEntry(
                            e.getRow(),
                            e.getCol() - 1,
                            e.getValue()
                        )
                    );
                }
            }
            numCols--;
        }
    }

    public static void main(String[] args) {
        SparseArray array = new SparseArray();

        System.out.println(array.removeColumn(1));
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Question 4: Interfaces & Polymorphism

Key Lessons

  • Interfaces define what, not how
  • Classes can implement interfaces
  • Polymorphism using lists of interfaces

Part (a): NumberGroup Interface

Code Runner Challenge

NUMBERGROUP

View IPYNB Source
// CODE_RUNNER: NUMBERGROUP

public class Main {

    interface NumberGroup {
        boolean contains(int num);
    }

    public static void main(String[] args) {}
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part (b): Range Class

Code Runner Challenge

RANGE

View IPYNB Source
// CODE_RUNNER: RANGE

public class Main {

    interface NumberGroup {
        boolean contains(int num);
    }

    static class Range implements NumberGroup {
        private int min, max;

        public Range(int min, int max) {
            this.min = min;
            this.max = max;
        }

        public boolean contains(int num) {
            return num >= min && num <= max;
        }
    }

    public static void main(String[] args) {}
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Part (c): MultipleGroups.contains

Lesson: Loop through interface references and delegate behavior.

Code Runner Challenge

CONTAINS

View IPYNB Source
// CODE_RUNNER: CONTAINS

import java.util.*;

public class Main {

    interface NumberGroup {
        boolean contains(int num);
    }

    static class MultipleGroups implements NumberGroup {
        private List<NumberGroup> groupList;

        public MultipleGroups(List<NumberGroup> groups) {
            groupList = groups;
        }

        public boolean contains(int num) {
            for (NumberGroup g : groupList) {
                if (g.contains(num)) {
                    return true;
                }
            }
            return false;
        }
    }

    public static void main(String[] args) {
        MultipleGroups group = new MultipleGroups();
        return;
    }
}

Main.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Score Review — AP Computer Science A

(Lesson-Based Scoring Breakdown)


Lesson 1: One-Dimensional Array Traversal (arraySum)

Intent

Calculate and return the sum of all values in a one-dimensional integer array.

Scoring (2 points)

Points Criterion
+1 Traverses the entire array exactly once using a loop (no bounds errors)
+1 Accumulates and returns the correct total of all array elements

Lesson 2: Two-Dimensional Arrays & Helper Methods (rowSums)

Intent

Return a one-dimensional array containing the sum of each row of a two-dimensional array.

Scoring (3 points)

Points Criterion
+1 Creates a one-dimensional result array with one element per row
+1 Correctly accesses each row of the two-dimensional array
+1 Uses the provided helper method arraySum to compute each row total

Lesson 3: Nested Loops & Uniqueness (isDiverse)

Intent

Determine whether all rows in a two-dimensional array have unique row sums.

Scoring (4 points)

Points Criterion
+1 Obtains row sums using a helper method
+1 Compares all row sums using nested loops
+1 Correctly identifies duplicate row sums
+1 Returns the correct boolean value in all cases

Lesson 4: String Traversal & Conditional Logic (HiddenWord)

Intent

Generate a hint string by comparing a guess to a hidden word.


Part 4a: Class Structure (3 points)

Points Criterion
+1 Defines the class and constructor with correct headers
+1 Declares an appropriate private instance variable
+1 Initializes the instance variable using the constructor parameter

Part 4b: getHint Method (6 points)

Points Criterion
+1 Traverses both the guess and hidden word using a loop (no bounds errors)
+1 Extracts individual characters using charAt
+1 Correctly detects exact position matches
+1 Correctly detects letters appearing elsewhere in the hidden word
+1 Adds exactly one correct character per position to the hint
+1 Returns the completed hint string

Lesson 5: Object Lists & Searching (getValueAt)

Intent

Return the value stored at a given position in a sparse array, or zero if none exists.

Scoring (2 points)

Points Criterion
+1 Searches the list of entries for matching row and column
+1 Returns stored value or 0 if no matching entry exists

Lesson 6: Safe Removal & Index Adjustment (removeColumn)

Intent

Remove a column from a sparse array and adjust remaining entries accordingly.

Scoring (4 points)

Points Criterion
+1 Removes all entries in the specified column
+1 Correctly shifts column indices for remaining entries
+1 Iterates safely through the list (no skipped entries)
+1 Updates the column count correctly

Lesson 7: Interfaces & Polymorphism (NumberGroup)

Intent

Define a common behavior shared by multiple numeric group types.

Scoring (1 point)

Points Criterion
+1 Defines an interface with exactly one correctly declared method

Lesson 8: Implementing Interfaces (Range)

Intent

Represent a contiguous range of integers.

Scoring (2 points)

Points Criterion
+1 Correctly implements the interface and constructor
+1 Accurately determines whether a value lies within the range

Lesson 9: Polymorphic Delegation (MultipleGroups.contains)

Intent

Determine whether a value exists in any of several number groups.

Scoring (2 points)

Points Criterion
+1 Traverses all number groups in the collection
+1 Returns true if any group contains the value

Course Timeline