AP Computer Science A Array Practice Exam

AP CSA Array Practice Exam

50 Questions • Beginner to Advanced

0 Correct | 0 Incorrect | 0/50 Answered
Section 1: Array Basics (Questions 1-10)
Question 1

What is printed?

int[] nums = {10, 20, 30, 40, 50}; System.out.println(nums[2]);
Question 2

What is printed?

int[] scores = {85, 90, 78, 92}; System.out.println(scores.length);
Question 3

What is printed?

int[] vals = {5, 10, 15, 20, 25}; System.out.println(vals[vals.length - 1]);
Question 4

What is printed?

int[] arr = new int[4]; arr[0] = 7; arr[2] = 3; System.out.println(arr[0] + " " + arr[1] + " " + arr[2]);
Question 5

What is printed?

int[] nums = {2, 4, 6}; for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " "); }
Question 6

What is printed?

int[] nums = {3, 7, 2, 8}; int sum = 0; for (int i = 0; i < nums.length; i++) { sum += nums[i]; } System.out.println(sum);
Question 7

What is printed?

int[] nums = {1, 2, 3, 4}; int total = 0; for (int n : nums) { total += n; } System.out.println(total);
Question 8

What is printed?

int[] nums = {5, 12, 8, 3, 15, 7}; int count = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] > 6) { count++; } } System.out.println(count);
Question 9

What is printed?

int[] nums = {4, 9, 2, 7, 5}; int max = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] > max) { max = nums[i]; } } System.out.println(max);
Question 10

What is printed?

int[] nums = {2, 4, 6, 8}; for (int i = 0; i < nums.length; i++) { nums[i] = nums[i] * 2; } System.out.println(nums[0] + " " + nums[3]);
Section 2: Searching & Finding (Questions 11-20)
Question 11

What is printed?

int[] nums = {8, 3, 12, 5, 1, 9}; int min = nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] < min) { min = nums[i]; } } System.out.println(min);
Question 12

What is printed? (Linear search with break)

int[] nums = {10, 25, 30, 25, 40}; int target = 25; int index = -1; for (int i = 0; i < nums.length; i++) { if (nums[i] == target) { index = i; break; } } System.out.println(index);
Question 13

What is printed? (No break)

int[] nums = {10, 25, 30, 25, 40}; int target = 25; int index = -1; for (int i = 0; i < nums.length; i++) { if (nums[i] == target) { index = i; } } System.out.println(index);
Question 14

What is printed?

int[] nums = {4, 8, 15, 16, 23}; int target = 15; boolean found = false; for (int n : nums) { if (n == target) { found = true; break; } } System.out.println(found);
Question 15

What is printed?

int[] nums = {80, 90, 85, 95}; int sum = 0; for (int n : nums) { sum += n; } double avg = (double) sum / nums.length; System.out.println(avg);
Question 16

What is printed?

int[] nums = {3, 8, 12, 7, 4, 9}; int even = 0, odd = 0; for (int n : nums) { if (n % 2 == 0) even++; else odd++; } System.out.println(even + " " + odd);
Question 17

What is printed?

int[] nums = {1, 2, 3, 4, 5}; for (int i = nums.length - 1; i >= 0; i--) { System.out.print(nums[i] + " "); }
Question 18

What is printed?

int[] nums = {3, 6, 9, 12, 15, 18}; int sum = 0; for (int n : nums) { if (n % 2 == 0) { sum += n; } } System.out.println(sum);
Question 19

What is printed after swapping first and last?

int[] nums = {10, 20, 30, 40, 50}; int temp = nums[0]; nums[0] = nums[nums.length - 1]; nums[nums.length - 1] = temp; System.out.println(nums[0] + " " + nums[4]);
Question 20

What is printed?

int[] nums = {12, 8, 16, 4, 20}; boolean allEven = true; for (int n : nums) { if (n % 2 != 0) { allEven = false; break; } } System.out.println(allEven);
Section 3: Shifting & Modifying (Questions 21-30)
Question 21

What is printed after shifting left?

int[] nums = {10, 20, 30, 40, 50}; int first = nums[0]; for (int i = 0; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; } nums[nums.length - 1] = first; System.out.println(nums[0] + " " + nums[1] + " " + nums[4]);
Question 22

What is printed after shifting right?

int[] nums = {10, 20, 30, 40, 50}; int last = nums[nums.length - 1]; for (int i = nums.length - 1; i > 0; i--) { nums[i] = nums[i - 1]; } nums[0] = last; System.out.println(nums[0] + " " + nums[1] + " " + nums[4]);
Question 23

What is printed?

int[] nums = {5, 3, 5, 7, 5, 9, 5}; int target = 5; int count = 0; for (int n : nums) { if (n == target) count++; } System.out.println(count);
Question 24

What is printed?

int[] nums = {3, 7, 12, 15, 20}; boolean sorted = true; for (int i = 0; i < nums.length - 1; i++) { if (nums[i] > nums[i + 1]) { sorted = false; break; } } System.out.println(sorted);
Question 25

What is the second largest value?

int[] nums = {12, 45, 7, 38, 23}; int max = nums[0], second = nums[0]; for (int n : nums) { if (n > max) { second = max; max = n; } else if (n > second && n != max) { second = n; } } System.out.println(second);
Question 26

What is printed after removing element at index 2?

int[] nums = {10, 20, 30, 40, 50}; int removeIndex = 2; for (int i = removeIndex; i < nums.length - 1; i++) { nums[i] = nums[i + 1]; } nums[nums.length - 1] = 0; System.out.println(nums[2] + " " + nums[3] + " " + nums[4]);
Question 27

What is printed after inserting 30 at index 2?

int[] nums = {10, 20, 40, 50, 0}; int insertIndex = 2, value = 30; for (int i = nums.length - 1; i > insertIndex; i--) { nums[i] = nums[i - 1]; } nums[insertIndex] = value; System.out.println(nums[2] + " " + nums[3] + " " + nums[4]);
Question 28

What is printed? (Deep copy)

int[] original = {5, 10, 15}; int[] copy = new int[original.length]; for (int i = 0; i < original.length; i++) { copy[i] = original[i]; } copy[0] = 99; System.out.println(original[0] + " " + copy[0]);
Question 29

What is printed? (Shallow copy trap)

int[] original = {5, 10, 15}; int[] copy = original; copy[0] = 99; System.out.println(original[0] + " " + copy[0]);
Question 30

What is printed after reversing in place?

int[] nums = {1, 2, 3, 4, 5}; int left = 0, right = nums.length - 1; while (left < right) { int temp = nums[left]; nums[left] = nums[right]; nums[right] = temp; left++; right--; } System.out.println(nums[0] + " " + nums[2] + " " + nums[4]);
Section 4: Advanced Patterns (Questions 31-40)
Question 31

Are these two arrays equal?

int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; boolean equal = true; if (a.length != b.length) { equal = false; } else { for (int i = 0; i < a.length; i++) { if (a[i] != b[i]) { equal = false; break; } } } System.out.println(equal);
Question 32

First 3 elements after merging sorted arrays?

int[] a = {1, 4, 7}; int[] b = {2, 5, 8}; int[] merged = new int[6]; int i = 0, j = 0, k = 0; while (i < a.length && j < b.length) { if (a[i] <= b[j]) { merged[k++] = a[i++]; } else { merged[k++] = b[j++]; } } // (remaining elements copied) System.out.println(merged[0] + " " + merged[1] + " " + merged[2]);
Question 33

How many consecutive duplicate pairs?

int[] nums = {1, 2, 2, 3, 4, 4, 4, 5}; int count = 0; for (int i = 0; i < nums.length - 1; i++) { if (nums[i] == nums[i + 1]) { count++; } } System.out.println(count);
Question 34

What is the longest streak?

int[] nums = {1, 2, 2, 2, 3, 3, 4}; int maxStreak = 1, currentStreak = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] == nums[i - 1]) { currentStreak++; if (currentStreak > maxStreak) maxStreak = currentStreak; } else { currentStreak = 1; } } System.out.println(maxStreak);
Question 35

Sum of elements from index 1 to 4?

int[] nums = {5, 10, 15, 20, 25, 30}; int start = 1, end = 4, sum = 0; for (int i = start; i <= end; i++) { sum += nums[i]; } System.out.println(sum);
Question 36

Is this a palindrome array?

int[] nums = {1, 2, 3, 2, 1}; boolean isPalindrome = true; int left = 0, right = nums.length - 1; while (left < right) { if (nums[left] != nums[right]) { isPalindrome = false; break; } left++; right--; } System.out.println(isPalindrome);
Question 37

First 3 elements after rotating right by 2?

int[] nums = {1, 2, 3, 4, 5}; int k = 2; int[] rotated = new int[5]; for (int i = 0; i < nums.length; i++) { int newIndex = (i + k) % nums.length; rotated[newIndex] = nums[i]; } System.out.println(rotated[0] + " " + rotated[1] + " " + rotated[2]);
Question 38

Find indices that sum to 9.

int[] nums = {2, 7, 11, 15}; int target = 9; int index1 = -1, index2 = -1; outer: for (int i = 0; i < nums.length; i++) { for (int j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] == target) { index1 = i; index2 = j; break outer; } } } System.out.println(index1 + " " + index2);
Question 39

How many unique elements?

int[] nums = {1, 1, 2, 2, 2, 3}; int[] result = new int[nums.length]; int count = 0; for (int i = 0; i < nums.length; i++) { boolean isDuplicate = false; for (int j = 0; j < count; j++) { if (nums[i] == result[j]) { isDuplicate = true; break; } } if (!isDuplicate) { result[count++] = nums[i]; } } System.out.println(count);
Question 40

What is printed?

int[] nums = {5, 3, 8, 1, 9}; boolean hasNegative = false; for (int n : nums) { if (n < 0) { hasNegative = true; break; } } System.out.println(hasNegative);
Section 5: Challenge Problems (Questions 41-50)
Question 41

What is printed?

int[] nums = {1, 2, 3, 4, 5}; int product = 1; for (int n : nums) { product *= n; } System.out.println(product);
Question 42

What is printed?

int[] nums = {10, 20, 30, 40}; int[] doubled = new int[nums.length]; for (int i = 0; i < nums.length; i++) { doubled[i] = nums[i] * 2; } System.out.println(nums[0] + " " + doubled[0]);
Question 43

What is printed?

int[] nums = {3, 1, 4, 1, 5, 9}; int countOnes = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] == 1) { countOnes++; } } System.out.println(countOnes);
Question 44

What is printed?

int[] nums = {5, 10, 15, 20, 25}; int sum = 0; for (int i = 0; i < nums.length; i += 2) { sum += nums[i]; } System.out.println(sum);
Question 45

What is printed?

int[] nums = {4, 7, 2, 9, 1}; int minIndex = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] < nums[minIndex]) { minIndex = i; } } System.out.println(minIndex);
Question 46

What is printed?

int[] nums = {2, 4, 6, 8, 10}; boolean allGreaterThan5 = true; for (int n : nums) { if (n <= 5) { allGreaterThan5 = false; break; } } System.out.println(allGreaterThan5);
Question 47

What is printed?

int[] nums = {1, 3, 5, 7, 9}; int target = 6; boolean found = false; for (int n : nums) { if (n == target) { found = true; } } System.out.println(found);
Question 48

What is printed?

int[] nums = {10, 20, 30}; int[] result = new int[nums.length + 1]; result[0] = 5; for (int i = 0; i < nums.length; i++) { result[i + 1] = nums[i]; } System.out.println(result[0] + " " + result[1] + " " + result[3]);
Question 49

What is printed?

int[] nums = {8, 3, 7, 1, 5}; int sumFirstLast = nums[0] + nums[nums.length - 1]; System.out.println(sumFirstLast);
Question 50

What is printed?

int[] nums = {2, 4, 6, 8, 10, 12}; int middle = nums.length / 2; int sum = nums[middle - 1] + nums[middle]; System.out.println(sum);

🎉 Exam Complete!

Review your answers above or click Reset to try again.

Contact form