Skip to the content.

2015 Practice FRQ Question 1 • 14 min read

Description

Analysis and breakdown of Question 1 from the 2015 AP Exam

Question 1: ARRAYS/ARRAYLISTS

image

Question breakdown and approach:

This question involves creating three static methods in a class named DiverseArray. The first method, arraySum calculates the sum of entries in a specified one-dimensional array. The second method, rowSums, calculates sums of each row in a given two-dimensional array and returns these sums in a one-dimensional array. The third method, isDiverse, determines if a given two-dimensional array is diverse, meaning no two rows have the same sum. Approach: (a) For arraySum, you need to iterate through the one-dimensional array and calculate the sum of its entries. (b) For rowSums, use the arraySum method appropriately. Iterate through each row of the two-dimensional array and use arraySum to calculate the sum of each row.(c) For isDiverse, use rowSums appropriately. Calculate the row sums for the given two-dimensional array and check if all row sums are unique, returning true if they are and false otherwise.

Part A:

Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.

public class ArraySum {

    public static void main(String[] args) {
        // Example usage:
        int[] arr1 = {1, 2, 3, 4, 5};
        int sum = arraySum(arr1);
        System.out.println("Sum of array elements: " + sum);
    }

    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }
}

ArraySum.main(null);
Sum of array elements: 15

PART B:

Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two-dimensional array arr2D of int values. The array is in row-major order: arr2D [r] [c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array.

public class RowSums {

    public static void main(String[] args) {
        // Example usage:
        int[][] arr2D = {
            {1, 2, 3},
            {4, 5, 6},
            {7, 8, 9}
        };

        int[] rowSums = rowSums(arr2D);
        for (int i = 0; i < rowSums.length; i++) {
            System.out.println("Sum of row " + i + ": " + rowSums[i]);
        }
    }

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

        for (int i = 0; i < numRows; i++) {
            for (int j = 0; j < arr2D[i].length; j++) {
                rowSums[i] += arr2D[i][j];
            }
        }

        return rowSums;
    }
}

RowSums.main(null);
Sum of row 0: 6
Sum of row 1: 15
Sum of row 2: 24

PART C:

A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum. Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse (mat1) returns true and the call isDiverse(mat2) returns false.

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        int[][] mat1 = {
            {1, 3, 2},
            {4, 3, 1},
            {2, 1, 4}
        };
        
        int[][] mat2 = {
            {1, 3, 2},
            {4, 3, 1},
            {1, 3, 2}
        };
        System.out.println("mat1 is diverse: " + isDiverse(mat1)); // true
        System.out.println("mat2 is diverse: " + isDiverse(mat2)); // false
    }
    public static boolean isDiverse(int[][] arr2D) {
        int[] rowSums = rowSums(arr2D);
        Arrays.sort(rowSums);
        for (int i = 0; i < rowSums.length - 1; i++) {
            if (rowSums[i] == rowSums[i + 1]) {
                return false;
            }
        }
        
        return true; 
    }
    public static int[] rowSums(int[][] arr2D) {
        int numRows = arr2D.length; 
        int[] sums = new int[numRows];

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

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

Main.main(null);
mat1 is diverse: true
mat2 is diverse: false

Summary:

In this question, the task was to implement a series of static methods that operate on both one dimensional and two dimensional arrays. Part (a) focused on creating a static method, arraySum, to calculate and return the sum of entries in a 1D array. Part (b) used a method, rowSums, to calculate the sums of each row in a 2D array and return them in a 1D array. Finally, part (c) required a new method, isDiverse, to determine if a given 2D array is diverse, meaning that no two rows have the same sum. I think I understood this FRQ pretty well as I’ve spent a lot of time studying Arrays and Array lists, when I examined some pretty similar questions during the FRQ Mini-lab earlier this year.