Logo

Collegeboard 2015 FRQs

Question 1

A static method in Java is a method that belongs to a class rather than an instance of the class, allowing it to be called directly on the class without creating an object. It is declared using the static keyword and cannot access instance variables or use the this keyword.

How to call a static method

Access_modifier static void methodName()
{ 
     // Method body.
} 
// Returns the sum of the entries in the one-dimensional array arr.

public static int arraySum (int [ ] arr){
    int sum = 0;

    // for every integer in array, add it to sum
    for(int i : arr)
        sum += i;
    
    return sum;
}

// Test
int[] array = {1, 3, 2, 7, 3};
System.out.print("Array: ");
System.out.println(Arrays.toString(array));

System.out.print("Sum: ");
System.out.print(arraySum(array));
Array: [1, 3, 2, 7, 3]
Sum: 16

Part B

// Returns a one-dimensional array in which the entry at index k is the sum of 
// the entries of row k of the two-dimensional array arr2D.

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

    int rowNum = 0;

    // Iterates through the rows of the 2D array
    for (int[] row: arr2D){

        // Uses the function written in Part A
        sums[rowNum] = arraySum(row);

        rowNum++;
    }

    return sums;
}

// Test
int[][] my2DArray = { {1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}};

System.out.print("Sum Array: ");
System.out.print(Arrays.toString(rowSums(my2DArray)));
Sum Array: [16, 32, 28, 20]

Part C

Reminder of how for loops work: Initialization: Executed once at the beginning of the loop. Condition: Checked before each iteration. If false, the loop exits. Update: Executed after each iteration.

for (initialization; condition; update) {
    // Code to be repeated
}
// Returns true if all rows in arr2D have different row sums; false otherwise.

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

    for (int i = 0; i < sums.length; i++) {
        
        // Iterate through each pair of rows starting from i+1
        for (int j = i + 1; j < sums.length; j++) {
            if (sums[i] == sums[j]) {
                return false;
            }
        }
    }
    return true;

}


// Testing
int[][] mat1 = { {1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}};
System.out.println("Sums of 2nd array: ");
System.out.println(Arrays.toString(rowSums(mat1)));
System.out.println("Is the first array diverse? ");
System.out.print(isDiverse(mat1));
System.out.println();
System.out.println();

int[][] mat2 = { {1, 1, 5, 3, 4}, {12, 7, 6, 2, 9}, {8, 11, 10, 2, 5}, {3, 2, 3, 0, 6}};
System.out.println("Sums of 2nd array: ");
System.out.println(Arrays.toString(rowSums(mat2)));
System.out.println("Is the first array diverse? ");
System.out.print(isDiverse(mat2));
Sums of 2nd array: 
[16, 32, 28, 20]
Is the first array diverse? 
true

Sums of 2nd array: 
[14, 36, 36, 14]
Is the first array diverse? 
false

Question 2

public class HiddenWord {
    private String word;

    public HiddenWord(String word) {
        this.word = word;
    }

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

        for (int i = 0; i < guess.length(); i++) {
            if (guess.charAt(i) == word.charAt(i)) {
                hint += guess.charAt(i);
            }
            else if (word.indexOf(guess.charAt(i)) != -1){
                hint += "+";
            }
            else{
                hint += "*";
            }
            // Add your other conditions if needed
        }

        return hint;
    }

    // Testing method
    public static void main(String[] args) {
        HiddenWord puzzle = new HiddenWord("HARPS");
        System.out.println(puzzle.getHint("HELLO"));
        System.out.println(puzzle.getHint("HEART"));
        System.out.println(puzzle.getHint("HARMS"));
        System.out.println(puzzle.getHint("HARPS"));
    }
}

HiddenWord.main(null);

H****
H*++*
HAR*S
HARPS

How indexOf to find character

  1. The indexOf method starts searching the string from the beginning (index 0).
  2. It examines each character in the string one by one.
  3. If the specified character (ch) is found, it returns the index of that character.
  4. If the character is not found, it returns -1.

Question 4

Part A


Part B


Part C