AP® practice test

Free AP® Computer Science A Practice Test (2026)

This is a set of 25 original practice questions that follow the official AP® Computer Science A course outline and its unit weights, each with a short explanation. The code is written in Java, in the style of the exam's code segments.

  • 25 questions
  • 4 topics from the official outline
  • No timer
Start the test

Free. Create a free account to see your score.

The practice test

25 questions

Question 1 of 25Consider the following code segment. int x = 0; if (x != 0 && 10 / x > 1) { System.out.println("A"); } else { System.out.println("B"); } What happens when the code segment is executed?
Question 2 of 25Consider the following code segment. int[] nums = {4, 9, 2, 7, 5}; int m = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] > nums[m]) { m = i; } } System.out.println(m); What is printed?
Question 3 of 25Consider the following code segment. int x = 17; int y = 5; double d = x / y + x % y; System.out.println(d); What is printed?
Question 4 of 25Consider the following code segment. ArrayList<Integer> list = new ArrayList<Integer>(); list.add(2); list.add(4); list.add(4); list.add(5); list.add(6); for (int i = 0; i < list.size(); i++) { if (list.get(i) % 2 == 0) { list.remove(i); } } System.out.println(list); What is printed?
Question 5 of 25Consider the following code segment. int n = 1; int count = 0; while (n < 100) { n = n * 3; count++; } System.out.println(count + " " + n); What is printed?
Question 6 of 25Consider the following class. public class Counter { private int count; public Counter(int start) { count = start; } public void add(int n) { count += n; } public int getCount() { return count; } } In a class other than Counter, the statement Counter c = new Counter(4); has been executed. Which of the following statements will compile without error?
Question 7 of 25Consider the following code segment. String s = "computer"; String t = s.substring(2, 5) + s.indexOf("t") + s.length(); System.out.println(t); What is printed?
Question 8 of 25Consider the following code segment. int[][] g = {{1, 2, 3}, {4, 5, 6}}; String out = ""; for (int c = 0; c < g[0].length; c++) { for (int r = 0; r < g.length; r++) { out += g[r][c] + " "; } } System.out.println(out); What is printed?
Question 9 of 25Assume that a and b are int variables that have been properly declared and initialized. Which of the following expressions is equivalent to !(a > 5 || b <= 2) for all values of a and b?
Question 10 of 25Consider the following method. public static int search(int[] arr, int target) { int low = 0; int high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; System.out.print(arr[mid] + " "); if (arr[mid] == target) { return mid; } else if (arr[mid] < target) { low = mid + 1; } else { high = mid - 1; } } return -1; } The array vals contains {3, 8, 12, 15, 21, 26, 30, 34, 40}. What is printed by the call search(vals, 34)?
Question 11 of 25Which of the following expressions evaluates to a random integer from 3 to 8, inclusive, with every value in that range possible?
Question 12 of 25Consider the following code segment. int sum = 0; for (int i = 3; i <= 20; i += 4) { sum += i; } System.out.println(sum); What is printed?
Question 13 of 25Consider the following class and method. public class Box { private int value; public Box(int v) { value = v; } public void setValue(int v) { value = v; } public int getValue() { return value; } } public static void update(Box b, int n) { b.setValue(n); b = new Box(0); b.setValue(n * 2); n = 100; } The following code segment appears in another method of the same class as update. Box box = new Box(1); int k = 3; update(box, k); System.out.println(box.getValue() + " " + k); What is printed?
Question 14 of 25Consider the following code segment, which is part of an insertion sort. int[] arr = {7, 3, 9, 1, 5}; for (int j = 1; j < arr.length; j++) { int temp = arr[j]; int k = j; while (k > 0 && arr[k - 1] > temp) { arr[k] = arr[k - 1]; k--; } arr[k] = temp; } What are the contents of arr after the body of the outer loop has executed three times (for j = 1, 2 and 3)?
Question 15 of 25Consider the following code segment. int count = 0; for (int r = 0; r < 4; r++) { for (int c = r; c < 4; c++) { count++; } } System.out.println(count); What is printed?
Question 16 of 25Consider the following code segment. String w = "data"; w.substring(1); w = w + "!"; System.out.println(w); What is printed?
Question 17 of 25Consider the following method. public static int f(int n) { if (n <= 1) { return 1; } return n + f(n / 2); } What value is returned by the call f(13)?
Question 18 of 25Consider the following code segment. String word = "planet"; String result = ""; for (int i = word.length() - 1; i >= 0; i -= 2) { result += word.substring(i, i + 1); } System.out.println(result); What is printed?
Question 19 of 25A line of a text file has been read into the String variable line, whose value is "Lee,17,Boston". Consider the following code segment. String[] parts = line.split(","); int age = Integer.parseInt(parts[1]); System.out.println(parts.length + age); What is printed?
Question 20 of 25Consider the following class. public class Ticket { private static int nextId = 1; private int id; public Ticket() { id = nextId; nextId++; } public int getId() { return id; } } The following code segment appears in another class. Ticket p = new Ticket(); Ticket q = new Ticket(); Ticket r = new Ticket(); System.out.println(p.getId() + " " + r.getId()); What is printed?
Question 21 of 25Consider the following code segment. int pts = 85; String g = ""; if (pts >= 90) { g += "A"; } else if (pts >= 80) { g += "B"; } if (pts >= 70) { g += "C"; } else { g += "F"; } System.out.println(g); What is printed?
Question 22 of 25A school wants a program that predicts which students need extra help in math. The developers train it only on records from students at one private school with small classes. Which of the following is the most likely problem when the program is used in other schools?
Question 23 of 25Consider the following method and code segment. public static int twice(int x) { x = x * 2; return x; } int v = 5; int w = twice(v); System.out.println(v + " " + w); What is printed?
Question 24 of 25Consider the following code segment. int n = 4072; int result = 0; while (n > 0) { result = result * 10 + n % 10; n /= 10; } System.out.println(result); What is printed?
Question 25 of 25Consider the following code segment. int[] vals = {1, 2, 3}; for (int v : vals) { v *= 10; } System.out.println(vals[0] + vals[2]); What is printed?

Your answers save on this device as you go.

Not sure where to start?

Ask Iris, our AI assistant, which exams your college takes and which guide fits. A person on our team answers at team@transfercredit.org.

These are practice questions we wrote from the official outline. This is not the real exam, and a practice score doesn't predict your AP score. We source all of this information from College Board. Each college decides whether to award credit for an AP exam, so confirm with your registrar before you book an exam. AP® is a registered trademark of the College Board, which was not involved in making these questions and does not endorse TransferCredit.org. Terms