AP CS a Array Arraylist 2DArrays 20 Question Intermediate
AP CS a Array Arraylist 2DArrays 20 Question Intermediate
Level Questions
Question Distribution:
7 questions - One-Dimensional Arrays (1D Arrays) → Written in the ArrayOperations class
7 questions - ArrayLists → Written in the ArrayListOperations class
6 questions - Two-Dimensional Arrays (2D Arrays) → Written in the MatrixOperations
class
Write a method that returns a new array containing only the even numbers from an int[] array.
Class: ArrayOperations
Example Usage:
// Output: 8 12 18
Write a method that finds the second largest number in an int[] array.
Class: ArrayOperations
Example Usage:
int[] numbers = {4, 1, 7, 8, 9};
System.out.println(ArrayOperations.findSecondLargest(numbers));
// Output: 8
Write a method that reverses an int[] array in place without using an extra array.
Class: ArrayOperations
Example Usage:
ArrayOperations.reverseArray(numbers);
// Output: 5 4 3 2 1
Write a method that rotates an int[] array to the right by a given number of positions.
Class: ArrayOperations
Example Usage:
ArrayOperations.rotateRight(numbers, 2);
}
// Output: 4 5 1 2 3
Write a method that finds the index of the first occurrence of a target number in an int[] array.
If the number is not found, return -1.
Class: ArrayOperations
Example Usage:
System.out.println(ArrayOperations.findIndex(numbers, 8));
// Output: 1
Write a method that merges two sorted int[] arrays into a single sorted array.
Class: ArrayOperations
Example Usage:
// Output: 1 2 3 4 5 6
Write a method that removes duplicate elements from an int[] array and returns a new array
with only unique values.
Class: ArrayOperations
Example Usage:
// Output: 1 2 3 4 5
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(3); list.add(7); list.add(3); list.add(4);
ArrayListOperations.removeOccurrences(list, 3);
System.out.println(list);
// Output: [7, 4]
Write a method that returns a new ArrayList<Integer> containing only the elements that
are greater than a given value.
Class: ArrayListOperations
Method Name: filterGreaterThan
Parameters: ArrayList<Integer> list, int value
Return Type: ArrayList<Integer>
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(2); list.add(5); list.add(9); list.add(12);
ArrayList<Integer> result = ArrayListOperations.filterGreaterThan(list, 5);
System.out.println(result);
// Output: [9, 12]
Write a method that shifts all elements of an ArrayList<String> to the left by one
position.
Class: ArrayListOperations
Method Name: shiftLeft
Parameters: ArrayList<String> list
Return Type: void
Example Usage:
ArrayList<String> words = new ArrayList<>();
words.add("a"); words.add("b"); words.add("c");
ArrayListOperations.shiftLeft(words);
System.out.println(words);
// Output: [b, c, a]
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(1); list.add(3); list.add(5); list.add(7);
System.out.println(ArrayListOperations.isSorted(list));
// Output: true
Write a method that returns a new ArrayList<String> containing only words with a
length greater than n.
Class: ArrayListOperations
Method Name: filterLongWords
Parameters: ArrayList<String> list, int n
Return Type: ArrayList<String>
Example Usage:
ArrayList<String> words = new ArrayList<>();
words.add("apple"); words.add("banana"); words.add("kiwi"); words.add("orange");
ArrayList<String> result = ArrayListOperations.filterLongWords(words, 5);
System.out.println(result);
// Output: [banana, orange]
Write a method that finds and returns the second smallest element in an
ArrayList<Integer>.
Class: ArrayListOperations
Method Name: findSecondSmallest
Parameters: ArrayList<Integer> list
Return Type: int
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(8); list.add(3); list.add(5); list.add(1); list.add(6);
System.out.println(ArrayListOperations.findSecondSmallest(list));
// Output: 3
Write a method that counts how many elements in an ArrayList<Integer> are divisible
by a given number.
Class: ArrayListOperations
Method Name: countDivisibleBy
Parameters: ArrayList<Integer> list, int divisor
Return Type: int
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(10); list.add(15); list.add(20); list.add(25); list.add(30);
System.out.println(ArrayListOperations.countDivisibleBy(list, 5));
// Output: 5
Write a method that returns the sum of all elements in a specific row of an int[][]
matrix.
Class: MatrixOperations
Method Name: sumRow
Parameters: int[][] matrix, int row
Return Type: int
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(MatrixOperations.sumRow(matrix, 1));
// Output: 4 + 5 + 6 = 15
Write a method that returns the sum of all elements in a specific column of an int[][]
matrix.
Class: MatrixOperations
Method Name: sumColumn
Parameters: int[][] matrix, int column
Return Type: int
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(MatrixOperations.sumColumn(matrix, 2));
// Output: 3 + 6 + 9 = 18
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
MatrixOperations.swapRows(matrix, 0, 2);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
// Output:
// 7 8 9
// 4 5 6
// 1 2 3
Write a method that checks if an int[][] matrix is a square matrix (same number of rows
and columns).
Class: MatrixOperations
Method Name: isSquareMatrix
Parameters: int[][] matrix
Return Type: boolean
Example Usage:
int[][] matrix1 = {{1, 2}, {3, 4}};
int[][] matrix2 = {{1, 2, 3}, {4, 5, 6}};
System.out.println(MatrixOperations.isSquareMatrix(matrix1));
// Output: true
System.out.println(MatrixOperations.isSquareMatrix(matrix2));
// Output: false
Write a method that returns true if an int[][] matrix is symmetric (i.e., matrix[i][j] ==
matrix[j][i]).
Class: MatrixOperations
Method Name: isSymmetric
Parameters: int[][] matrix
Return Type: boolean
Example Usage:
int[][] matrix1 = {{1, 2, 3}, {2, 4, 5}, {3, 5, 6}};
int[][] matrix2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(MatrixOperations.isSymmetric(matrix1));
// Output: true
System.out.println(MatrixOperations.isSymmetric(matrix2));
// Output: false
Write a method that finds the largest element in the diagonal of an int[][] matrix.
Class: MatrixOperations
Method Name: findMaxInDiagonal
Parameters: int[][] matrix
Return Type: int
Example Usage:
int[][] matrix = {{3, 1, 4}, {2, 8, 6}, {5, 9, 7}};
System.out.println(MatrixOperations.findMaxInDiagonal(matrix));
// Output: 9