Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Tri 2 FRQ finals • 14 min read

  • 3
  • 4

  • 1

    a.

    Screenshot 2024-02-20 at 12 43 21 PM

    Answer:

    public static int arraySum(int[] arr) {
      // Initialize a variable to hold the sum
      int sum = 0;
    
      // Iterate over each element in the array
      for (int i = 0; i < arr.length; i++) {
        sum += arr[i];
      }
    
      return sum;
    }
    
    
    int[] arr = {1, 2, 3, 4, 5};
    int sum = arraySum(arr);
    System.out.println("The sum of the array is: " + sum);
    
    

    code output:

    The sum of the array is: 15
    

    b.

    Screenshot 2024-02-20 at 12 44 10 PM

    Answer:

    Answer:

    public static int[] rowSums(int[][] arr2D) {
      int numRows = arr2D.length;  // Get the number of rows in the 2D array
      int[] sums = new int[numRows]; // Create a new 1D array to store the row sumsw
    
      for (int i = 0; i < numRows; i++) {
        sums[i] = arraySum(arr2D[i]); // Use the arraySum method to calculate the sum of each row and store it in the sums array
      }
    
      return sums;
    }
    
    //2d array 1-9 but i cant commit if it is here, so just imagine it divided into three brackets 
    int[] sums = rowSums(arr2D);
    
    for (int i = 0; i < sums.length; i++) {
      System.out.println("The sum of row " + i + " is: " + sums[i]);
    }
    
    The sum of row 0 is: 6
    The sum of row 1 is: 15
    The sum of row 2 is: 24
    

    c.

    Screenshot 2024-02-20 at 12 46 01 PM

    Answer:

    public static boolean isDiverse(int[][] arr2D) {
      // 1. Calculate the row sums and store them in a separate array.
      int[] rowSums = rowSums(arr2D);
    
      // 2. Check for duplicate sums using a nested loop.
      for (int i = 0; i < rowSums.length - 1; i++) {
        for (int j = i + 1; j < rowSums.length; j++) {
          if (rowSums[i] == rowSums[j]) {
            return false; // If a duplicate sum is found, the array is not diverse.
          }
        }
      }
    
      // 3. If no duplicates are found, return true (array is diverse).
      return true;
    }
    
    

    2

    Screenshot 2024-02-20 at 12 47 58 PM

    Answer:

    public class HiddenWord {
        // A private instance variable that holds the hidden word
        private String hiddenWord;
    
        // A constructor that takes a String argument to initialize the hidden word
        public HiddenWord(String hiddenWord) {
            this.hiddenWord = hiddenWord;
        }
    
        // A method that takes a guess and returns a hint based on the guess
        public String getHint(String guess) {
            StringBuilder hint = new StringBuilder();
    
            // Iterate over each character in the hidden word
            for (int i = 0; i < hiddenWord.length(); i++) {
                // Get the corresponding characters from the guess and the hidden word
                char guessChar = guess.charAt(i);
                char hiddenChar = hiddenWord.charAt(i);
    
                // If the guessed character matches the hidden character at the same position
                if (guessChar == hiddenChar) {
                    hint.append(guessChar);
                } 
                // If the guessed character is in the hidden word but at a different position
                else if (hiddenWord.indexOf(guessChar) != -1) {
                    hint.append("+");
                } 
                // If the guessed character is not in the hidden word
                else {
                    hint.append("*");
                }
            }
    
            return hint.toString();
        }
    }
    
    

    3

    Assignment name: 2015 Practice Exam FRQ: Question 3: Part a: getValueAt

    a.

    Answer:

    public int getValueAt(int row, int col) {
        for (SparseArrayEntry entry : entries) {
            if (entry.getRow() == row && entry.getCol() == col) {
                return entry.getValue();
            }
        }
        return 0;  // return 0 if no entry found at the specified row and column
    }
    
    
    

    b.

    public void removeColumn(int col) {
        // create an iterator for the 'entries' collection
        Iterator<SparseArrayEntry> iterator = entries.iterator();
    
        // iterate over each entry in the 'entries' collection
        while (iterator.hasNext()) {
            // get the current entry
            SparseArrayEntry entry = iterator.next();
    
            // if the column of the current entry matches the column to be removed
            if (entry.getCol() == col) {
                iterator.remove();
            } 
            // if the column of the current entry is greater than the column to be removed
            else if (entry.getCol() > col) {
                // create a new entry with the same row and value as the current entry,
                // but with the column decremented by 1
                int newRow = entry.getRow();
                int newCol = entry.getCol() - 1;
                int newValue = entry.getValue();
    
                iterator.remove();
    
                // add the new entry to the 'entries' collection
                entries.add(new SparseArrayEntry(newRow, newCol, newValue));
            }
        }
    
        numCols--;
    }
    
    

    4

    a.

    // This is the NumberGroup interface declaration.
    public interface NumberGroup {
    
        // The contains method takes an integer as input and returns 
        // a boolean indicating whether or not the given integer is 
        // contained within the number group.
        boolean contains(int number);
    }
    

    b.

    // The Range class implements the NumberGroup interface.
    public class Range implements NumberGroup {
        // Instance variables to store the minimum and maximum values of the range.
        private int min;
        private int max;
    
        // Constructor that takes the minimum and maximum values of the range.
        public Range(int min, int max) {
            this.min = min;
            this.max = max;
        }
    
        // The contains method checks if a given integer is within the range.
        public boolean contains(int number) {
            return number >= min && number <= max;
        }
    }
    

    c.

    public boolean contains(int num) {
        // Iterate over each NumberGroup in groupList
        for (NumberGroup group : groupList) {
            // Check if the number is contained within the current group
            if (group.contains(num)) {
                // If the number is found, return true immediately
                return true;
            }
        }
        // If the number is not found in any group, return false
        return false;
    }