AP CS a Array Arraylist 2DArrays 20 Question Basic
AP CS a Array Arraylist 2DArrays 20 Question Basic
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 prints all elements of an int[] array to the console.
Class: ArrayOperations
Method Name: printArray
Parameters: int[] arr
Return Type: void
Example Usage:
int[] numbers = {1, 2, 3, 4, 5};
ArrayOperations.printArray(numbers);
// Output: 1 2 3 4 5
Write a method that returns the sum of all elements in a double[] array.
Class: ArrayOperations
Method Name: sumArray
Parameters: double[] arr
Return Type: double
Example Usage:
double[] values = {1.5, 2.5, 3.0};
System.out.println(ArrayOperations.sumArray(values));
// Output: 7.0
Example Usage:
int[] numbers = {10, 25, 30, 5};
System.out.println(ArrayOperations.findMax(numbers));
// Output: 30
Write a method that counts how many times a given number appears in an int[] array.
Class: ArrayOperations
Method Name: countOccurrences
Parameters: int[] arr, int target
Return Type: int
Example Usage:
int[] numbers = {4, 5, 6, 5, 7, 5};
System.out.println(ArrayOperations.countOccurrences(numbers, 5));
// Output: 3
Write a method that prints all words in a String[] array in reverse order.
Class: ArrayOperations
Method Name: reversePrintArray
Parameters: String[] arr
Return Type: void
Example Usage:
String[] words = {"apple", "banana", "cherry"};
ArrayOperations.reversePrintArray(words);
// Output: cherry banana apple
Write a method that prints only the odd numbers in an int[] array.
Class: ArrayOperations
Method Name: printOddNumbers
Parameters: int[] arr
Return Type: void
Example Usage:
int[] numbers = {1, 2, 3, 4, 5};
ArrayOperations.printOddNumbers(numbers);
// Output: 1 3 5
Write a method that returns the sum of even numbers in a given int[] array.
Class: ArrayOperations
Method Name: sumEvenNumbers
Parameters: int[] arr
Return Type: int
Example Usage:
int[] numbers = {2, 3, 4, 5, 6};
System.out.println(ArrayOperations.sumEvenNumbers(numbers));
// Output: 12
Unit 7: ArrayLists (ArrayListOperations Class, 7 Questions)
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(10); list.add(20); list.add(30);
ArrayListOperations.printList(list);
// Output: 10 20 30
Example Usage:
ArrayList<String> words = new ArrayList<>();
words.add("Hello");
ArrayListOperations.addWord(words, "World");
System.out.println(words);
// Output: [Hello, World]
Example Usage:
ArrayList<Double> list = new ArrayList<>();
list.add(2.0); list.add(4.0); list.add(6.0);
System.out.println(ArrayListOperations.calculateAverage(list));
// Output: 4.0
Example Usage:
ArrayList<String> words = new ArrayList<>();
words.add("apple"); words.add("banana"); words.add("kiwi");
System.out.println(ArrayListOperations.findLongestWord(words));
// Output: banana
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(1); list.add(2); list.add(3); list.add(4);
ArrayListOperations.removeOddNumbers(list);
System.out.println(list);
// Output: [2, 4]
Example Usage:
ArrayList<Integer> list = new ArrayList<>();
list.add(12); list.add(7); list.add(9); list.add(15);
System.out.println(ArrayListOperations.findMaxValue(list));
// Output: 15
Unit 8: 2D Arrays (MatrixOperations Class, 6 Questions)
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
MatrixOperations.printMatrix(matrix);
// Output:
// 1 2 3
// 4 5 6
// 7 8 9
Write a method that returns the sum of all elements in an int[][] matrix.
Class: MatrixOperations
Method Name: sumMatrix
Parameters: int[][] matrix
Return Type: int
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(MatrixOperations.sumMatrix(matrix));
// Output: 45
Example Usage:
int[][] matrix = {{3, 8, 1}, {7, 2, 6}, {5, 4, 9}};
System.out.println(MatrixOperations.findMaxValue(matrix));
// Output: 9
Write a method that prints the sum of each row in an int[][] matrix.
Class: MatrixOperations
Method Name: printRowSums
Parameters: int[][] matrix
Return Type: void
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}};
MatrixOperations.printRowSums(matrix);
// Output:
// Row 0 sum: 6
// Row 1 sum: 15
Write a method that prints the elements along the diagonal of an int[][] matrix.
Class: MatrixOperations
Method Name: printDiagonal
Parameters: int[][] matrix
Return Type: void
Example Usage:
int[][] matrix = {{1, 0, 0}, {0, 2, 0}, {0, 0, 3}};
MatrixOperations.printDiagonal(matrix);
// Output: 1 2 3
Write a method that counts how many times a given number appears in an int[][]
matrix.
Class: MatrixOperations
Method Name: countOccurrences
Parameters: int[][] matrix, int target
Return Type: int
Example Usage:
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 5}};
System.out.println(MatrixOperations.countOccurrences(matrix, 5));
// Output: 2