(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 static int arraySum (int [ ] arr){
    int sum = 0;
    for (int i = 0; i<arr.length; i++){
        sum+= arr[i];
    }
    return sum;
}

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

(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 static int [ ] rowSums(int [ ] [ ] arr2D){
    int rowsAmount = arr2D.length;
    int[] sums = new int[rowsAmount];
    for (int i = 0; i<arr2D.length; i++){
        sums[i]= arraySum(arr2D[i]);
    }
    return sums;
}
public class TestB {
    public static void main(String[] args) {
        int[][] arr2D = {
        {1,3,2,7,3},
        {10,10,4,6,2},
        {5,3,5,9,6},
        {7,6,4,2,1}};
        int[] sums = rowSums(arr2D);
        for (int i = 0; i<sums.length; i++){
            System.out.println("Row "+i+" Sum: "+sums[i]);
        }
    }
}
TestB.main(null);
Row 0 Sum: 16
Row 1 Sum: 32
Row 2 Sum: 28
Row 3 Sum: 20

(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.

public static boolean isDiverse(int [ ] [ ] arr2D){
    int[] sums = rowSums(arr2D);
    for (int i = 0; i<sums.length-1; i++){
        for (int j = i+1; j<sums.length; j++){
            if(sums[i]==sums[j]){
                return false;
            }
        }
    }
    return true;
}
public class TestC {
    public static void main(String[] args) {
        int[][] mat1 = {
            {1,3,2,7,3},
            {10,10,4,6,2},
            {5,3,5,9,6},
            {7,6,4,2,1}};
        int[][] mat2 = {
            {1,1,5,3,4},
            {12,7,6,1,9},
            {8,11,10,2,5},
            {3,2,3,0,6}};
        System.out.println(isDiverse(mat1)+": mat1 is diverse");
        System.out.println(isDiverse(mat2)+": mat2 is diverse");
    }
}
TestC.main(null);
true: mat1 is diverse
false: mat2 is diverse

This FRQ question regards the topic of Array/ArrayList and 2D Array. In part a, this question tested our knowledge on arrays because it requires the sum of all elements in the array. In part b, this question was about both arrays and 2D arrays because we had to navigate through a 2D array using iterative loops to then find the sums of each 1D array within the 2D array. Lastly, part c tests our ability on 2D arrays and 1D arrays as well but it was mostly 1D arrays because we need to compare the elements in the array to see if there we duplicates.