[go: up one dir, main page]

0% found this document useful (0 votes)
4 views8 pages

AP CS a Array Arraylist 2DArrays 20 Question Intermediate

The document outlines 20 intermediate-level questions for AP Computer Science A covering Arrays, ArrayLists, and 2D Arrays across three units. Unit 6 focuses on One-Dimensional Arrays with 7 questions, Unit 7 on ArrayLists with another 7 questions, and Unit 8 on Two-Dimensional Arrays with 6 questions. Each question includes a method name, parameters, return type, and example usage.

Uploaded by

ovgutoprak2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

AP CS a Array Arraylist 2DArrays 20 Question Intermediate

The document outlines 20 intermediate-level questions for AP Computer Science A covering Arrays, ArrayLists, and 2D Arrays across three units. Unit 6 focuses on One-Dimensional Arrays with 7 questions, Unit 7 on ArrayLists with another 7 questions, and Unit 8 on Two-Dimensional Arrays with 6 questions. Each question includes a method name, parameters, return type, and example usage.

Uploaded by

ovgutoprak2008
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

AP Computer Science A - Unit 6, 7, and 8 (Arrays, ArrayLists, 2D Arrays) - 20 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

Unit 6: 1D Arrays (ArrayOperations Class, 7 Questions)

Write a method that returns a new array containing only the even numbers from an int[] array.

Class: ArrayOperations

Method Name: filterEvenNumbers

Parameters: int[] arr

Return Type: int[]

Example Usage:

int[] numbers = {3, 8, 12, 5, 7, 18};

int[] result = ArrayOperations.filterEvenNumbers(numbers);

for (int num : result) {

System.out.print(num + " ");

// Output: 8 12 18

Write a method that finds the second largest number in an int[] array.

Class: ArrayOperations

Method Name: findSecondLargest

Parameters: int[] arr

Return Type: int

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

Method Name: reverseArray

Parameters: int[] arr

Return Type: void

Example Usage:

int[] numbers = {1, 2, 3, 4, 5};

ArrayOperations.reverseArray(numbers);

for (int num : numbers) {

System.out.print(num + " ");

// 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

Method Name: rotateRight

Parameters: int[] arr, int positions

Return Type: void

Example Usage:

int[] numbers = {1, 2, 3, 4, 5};

ArrayOperations.rotateRight(numbers, 2);

for (int num : numbers) {

System.out.print(num + " ");

}
// 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

Method Name: findIndex

Parameters: int[] arr, int target

Return Type: int

Example Usage:

int[] numbers = {5, 8, 2, 9, 8};

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

Method Name: mergeSortedArrays

Parameters: int[] arr1, int[] arr2

Return Type: int[]

Example Usage:

int[] arr1 = {1, 3, 5};

int[] arr2 = {2, 4, 6};

int[] merged = ArrayOperations.mergeSortedArrays(arr1, arr2);

for (int num : merged) {

System.out.print(num + " ");

// 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

Method Name: removeDuplicates

Parameters: int[] arr

Return Type: int[]

Example Usage:

int[] numbers = {1, 2, 2, 3, 4, 4, 5};

int[] unique = ArrayOperations.removeDuplicates(numbers);

for (int num : unique) {

System.out.print(num + " ");

// Output: 1 2 3 4 5

Unit 7: ArrayLists (ArrayListOperations Class, 7 Questions)

Write a method that removes all occurrences of a given number from an


ArrayList<Integer>.
Class: ArrayListOperations
Method Name: removeOccurrences
Parameters: ArrayList<Integer> list, int value
Return Type: void

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]

Write a method that returns true if an ArrayList<Integer> is sorted in ascending order.


Class: ArrayListOperations
Method Name: isSorted
Parameters: ArrayList<Integer> list
Return Type: boolean

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

Unit 8: 2D Arrays (MatrixOperations Class, 6 Questions)

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

Write a method that swaps two rows in an int[][] matrix.


Class: MatrixOperations
Method Name: swapRows
Parameters: int[][] matrix, int row1, int row2
Return Type: void

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

You might also like