Arrays Practice Questions

AP (Advanced Placement) · AP Computer Science A · 150 free MCQs with instant results and detailed explanations.

150
Total
46
Easy
77
Medium
27
Hard

Start Practicing Arrays

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Arrays

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What will be the output of the following code snippet? int[] numbers = {1, 2, 3, 4}; System.out.println(numbers[2]);
A. 1
B. 2
C. 3
D. 4
Show Answer & Explanation
Correct Answer: C
Arrays in Java are zero-indexed, meaning the first element is at index 0. Therefore, numbers[2] accesses the third element, which is 3.
Q2
Easy
Given the array int[] values = {5, 10, 15, 20}; what is the value of values.length?
A. 3
B. 4
C. 5
D. 10
Show Answer & Explanation
Correct Answer: B
The length attribute of an array in Java returns the total number of elements in the array. Since there are four elements, values.length is 4.
Q3
Easy
Consider the following code: int[] arr = {2, 4, 6, 8}; arr[1] = arr[0] + arr[2]; What will the array 'arr' look like after execution?
A. [2, 4, 6, 8]
B. [2, 8, 6, 8]
C. [2, 8, 6, 10]
D. [2, 6, 6, 8]
Show Answer & Explanation
Correct Answer: B
After executing the statement, arr[1] is updated to the sum of arr[0] and arr[2], which is 2 + 6 = 8. The updated array is therefore [2, 8, 6, 8].
Q4
Medium
What will be the output of the following code snippet? ```java int[] arr = {5, 10, 15, 20}; System.out.println(arr[1] + arr[2]); ```
A. 25
B. 30
C. 10
D. 15
Show Answer & Explanation
Correct Answer: B
The code adds the second element (10) and the third element (15) from the array, resulting in 25.
Q5
Medium
Consider the following code snippet: ```java int[] nums = {2, 4, 6, 8}; for(int i = 0; i < nums.length; i++) { nums[i] *= 2; } ``` What will be the value of nums[3] after execution?
A. 12
B. 16
C. 8
D. 10
Show Answer & Explanation
Correct Answer: B
The loop multiplies each element of the array by 2. Thus, nums[3] (which was initially 8) becomes 16 after the operation.
Q6
Medium
If an array `scores` is declared as `int[] scores = new int[5];`, what will be the default value of scores[2]?
A. 0
B. 1
C. null
D. -1
Show Answer & Explanation
Correct Answer: A
In Java, when an integer array is created, all elements are initialized to 0 by default.
Q7
Medium
Which of the following statements correctly modifies the first element of an array `data` declared as `int[] data = {1, 2, 3};`?
A. data[1] = 10;
B. data[0] = 10;
C. data[2] = 10;
D. data[3] = 10;
Show Answer & Explanation
Correct Answer: B
To modify the first element of the array, you must access it using index 0, thus data[0] = 10; is correct.
Q8
Hard
What is the result of the following code snippet? int[] arr = {1, 2, 3, 4, 5}; int sum = 0; for(int i = 0; i < arr.length; i++) { sum += arr[i]; } return sum;
A. 10
B. 15
C. 20
D. 25
Show Answer & Explanation
Correct Answer: B
The sum of the elements in the array {1, 2, 3, 4, 5} is calculated as 1 + 2 + 3 + 4 + 5 = 15, making option B the correct answer.
Q9
Hard
Given an array of integers, how would you determine if it contains a duplicate element? Which algorithm provides an efficient solution?
A. Sort the array and check adjacent elements.
B. Use a nested loop to compare each pair of elements.
C. Use a HashSet to track seen elements.
D. Reverse the array and check for duplicates.
Show Answer & Explanation
Correct Answer: C
Using a HashSet allows you to track seen elements in O(1) time complexity for insertions and lookups. This is more efficient than sorting or using nested loops, leading to an overall time complexity of O(n).
Q10
Hard
Consider the following method that takes an array of integers. What is the output of the method if the input array is {1, 2, 3, 4, 5}?
A. 5
B. 15
C. 10
D. 0
Show Answer & Explanation
Correct Answer: B
The method sums up all the elements in the array. For the array {1, 2, 3, 4, 5}, the sum is 1 + 2 + 3 + 4 + 5 = 15.

Showing 10 of 150 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 150 Questions →

Arrays โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 150 practice MCQs for the chapter Arrays in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 46 easy, 77 medium, 27 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.