AP CSA Practice Exam 2
AP Computer Science A
Full Practice Exam #2
42 Multiple-Choice Questions • 2025-2026 Curriculum • Units 1-4
Error-Spotting • I/II/III Analysis • Output Tracing • 90 Minutes
Test Mode
Full timed exam — 90 minutes
Submit all answers at the end
Mimics real AP exam conditions
Study Mode
Check each answer immediately
See full explanation after each question
Self-paced, no timer
int a = 7; int b = 2; double avg = a / b; System.out.println(avg);
7 / 2 evaluates to 3 (int), then widens to 3.0. Fix: (double) a / b or a / (double) b.String word = "Computer"; System.out.println(word.substring(0, 4).toUpperCase());
substring(0,4) extracts indices 0-3: "Comp". toUpperCase() gives "COMP". The end index is exclusive.result?int x = 17; int result = (x / 5) * 5 + (x % 5);
x/5 = 3; 3*5 = 15; x%5 = 2; 15+2 = 17. This expression always reconstructs the original integer value.double p = -4.2; double q = 1.8; Which of the following evaluate to a value greater than 4?Math.abs(p)Math.pow(q, 3)Math.sqrt(25.0)
|-4.2| = 4.2 > 4. II: 1.8^3 = 5.832 > 4. III: sqrt(25) = 5.0 > 4. All three exceed 4.String name = null; int len = name.length(); System.out.println(len);
null to a reference variable compiles fine. Calling any instance method on null throws a NullPointerException at runtime.int m = 3;
int n = 4;
System.out.println("Sum: " + m + n);
System.out.println("Sum: " + (m + n));
"Sum: " + 3 = "Sum: 3", then + 4 = "Sum: 34". Parentheses in line 2 force arithmetic first: (3+4)=7, giving "Sum: 7".z?double d = 9.99; int z = (int)(d * 10) % 7;
d*10 = 99.9. Cast truncates to 99. 99 % 7 = 1 (since 14x7=98, 99-98=1).int alpha = 10; int beta = 3; alpha -= beta; beta *= alpha; alpha += beta;
-
alphaequals 28 -
betaequals 21 -
alpha + betaequals 49
alpha-=3 gives 7. beta*=7 gives 21. alpha+=21 gives 28. All three statements are true: alpha=28, beta=21, sum=49.(int)(Math.random() * range) + min. B gives [5,11]; C gives [5,16]; D gives [4,11].public static int doubleAbs(int val) {
if (val < 0) {
val = -val;
}
val = val * 2;
}
int but no return statement exists — compile error. Reassigning parameters is perfectly legal in Java.score = 85?int score = 85;
String grade;
if (score >= 90) {
grade = "A";
}
if (score >= 80) {
grade = "B";
}
if (score >= 70) {
grade = "C";
}
else { grade = "F"; }
System.out.println(grade);
if runs independently. score=85: first if skips, second sets grade="B", third sets grade="C". The else pairs only with the third if and does not run since 85>=70. Final output: C. Fix: use else if.!(x > 5 && y <= 10)?x <= 5 || y > 10!(x > 5) || !(y <= 10)x <= 5 && y > 10
!(A && B) = !A || !B. I and II are both equivalent. III uses && instead of || — not equivalent.int ctr = 1, total = 0;
while (ctr <= 5) {
if (ctr % 2 != 0) {
total += ctr;
}
ctr++;
}
System.out.println(total);
n inclusive?i = n-1, missing value n. Options A, B, and D all correctly cover 1 through n.int count = 0;
for (int r = 0; r < 4; r++) {
for (int c = r; c < 4; c++) {
count++;
}
}
System.out.println(count);
for loop computes 6! (720). Which while loops produce the same result?int product = 1;
for (int k = 1; k <= 6; k++) { product *= k; }
int k = 1, product = 1; while (k <= 6) { product *= k; k++; }int k = 1, product = 1; while (k < 7) { k++; product *= k; }int k = 0, product = 1; while (k < 6) { k++; product *= k; }
mystery("abcde") return?public static String mystery(String s) {
String result = "";
for (int i = s.length() - 1; i >= 0; i -= 2) {
result += s.charAt(i);
}
return result;
}
getValue() has a side effect (it prints "called"). With x = 0, this runs:boolean r = (x != 0) && (getValue() > 0);
-
risfalse -
getValue()is NOT called due to short-circuit evaluation - "called" is printed to the console
x=0 makes x != 0 false. && short-circuits: right side never evaluates. So r=false (I true), getValue() not called (II true), "called" not printed (III false).n != 0 is always true. Option D terminates correctly at n=0.int p = 6, q = 4;
if (p > 5) {
if (q > 5) {
System.out.println("A");
}
else { System.out.println("B"); }
} else if (p > 3) {
System.out.println("C");
} else {
System.out.println("D");
}
B.int n = 16;
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
System.out.println(count);
public class Thermometer {
private double temperature;
public Thermometer(double temperature) {
temperature = temperature;
}
}
this.temperature = temperature;
Counter c1 = new Counter(); Counter c2 = c1; c1.increment(); c1.increment(); Which statements are TRUE?-
c1.getCount()returns2 -
c2.getCount()returns0 -
c1andc2refer to the same object in memory
c2 = c1 copies the reference — both point to the same object (III true). After two increments, both return 2. So I is true; II is false (c2.getCount() = 2, not 0).private?public class BankAccount {
private double balance;
public boolean isRich(double threshold) {
if (balance > threshold) {
return true;
} else if (balance < threshold) {
return false;
}
}
}
balance == threshold neither branch executes — no return statement. Java compiler requires all paths to return a value. Fix: add return false; after the if-else.Dog class overrides toString() to return name + " (" + age + ")". What is printed?Dog d = new Dog("Rex", 3);
System.out.println(d);
println(obj) calls toString() automatically. Since it's overridden, output is Rex (3). Without the override, C would occur.Rectangle r = new Rectangle(4, 6) with private fields width=4, height=6. Which statements are TRUE?-
r.area()returns24 -
r.perimeter()returns20 -
r.widthcan be accessed directly from client code
width is private — direct access from client code causes a compile error.setGPA(double g) setter should reject values outside [0.0, 4.0]. Which implementation is CORRECT?|| which is always true for any finite number. Only B correctly accepts the closed interval [0.0, 4.0].public class Coin {
private String side = "heads";
public void flip() {
side = (Math.random() < 0.5) ? "tails" : "heads";
}
public static String getSide() {
return side;
}
}
static method cannot access non-static (instance) fields. Fix: remove static from getSide().public class Multiplier {
private int factor;
public Multiplier(int f) {
factor = f;
}
public int apply(int n) {
return n * factor;
}
public Multiplier combine(Multiplier other) {
return new Multiplier(factor * other.factor);
}
}
// Client:
Multiplier triple = new Multiplier(3);
Multiplier dbl = new Multiplier(2);
Multiplier six = triple.combine(dbl);
System.out.println(six.apply(5));
triple.combine(dbl) creates Multiplier(3x2=6). six.apply(5) = 5x6 = 30. Private fields are accessible within the same class, including via a parameter reference.public Car(String make, int year). Which create a valid Car object?Car c = new Car("Toyota", 2024);Car c = new Car(2024, "Toyota");Car c = new Car("Honda", 1999);
String s1 = new String("hello");
String s2 = new String("hello");
if (s1 == s2) {
System.out.println("equal");
} else {
System.out.println("not equal");
}
== compares references. new String("hello") creates two separate heap objects, so == is false. Use .equals() to compare content.int[] nums = {10, 20, 30, 40, 50};
for (int i = 0; i <= nums.length; i++) {
System.out.println(nums[i]);
}
i <= nums.length allows i=5. Valid indices are 0-4, so nums[5] throws ArrayIndexOutOfBoundsException. Fix: i < nums.length.int[] data = {3, 1, 4, 1, 5, 9, 2, 6};
int maxVal = data[0];
for (int val : data) {
if (val > maxVal) {
maxVal = val;
}
}
System.out.println(maxVal);
int[] scores = {70, 80, 90};
for (int s : scores) {
s = s * 2;
}
System.out.println(scores[0]);
for (int i=0; i
ArrayList words starts as ["cat","dog","bird"]. After words.add(1,"fish"); then words.remove(2); which are TRUE?-
words.size()returns3 -
words.get(0)returns"cat" -
words.get(1)returns"dog"
ArrayListnums = new ArrayList<>(Arrays.asList(1,2,3,4,5,6)); for (int i = 0; i < nums.size(); i++) { if (nums.get(i) % 2 == 0) { nums.remove(i); } }
int[][] grid = {{1,2,3},{4,5,6},{7,8,9}};
int total = 0;
for (int r = 0; r < grid.length; r++) {
total += grid[r][r];
}
System.out.println(total);
- Arrays have a fixed size; ArrayLists can grow and shrink dynamically.
- Arrays can hold primitive types; ArrayLists require wrapper classes such as
Integer. - ArrayLists are faster than arrays when accessing elements by index.
ArrayListlist = new ArrayList<>(); list.add(10); list.add(20); list.add(30); list.add(0, 5); list.set(2, 25); list.remove(3);
public static int countTarget(int[][] matrix, int target) {
int count = 0;
for (int r = 0; r < matrix.length; r++) {
for (int c = 0; c < matrix.length; c++) {
if (matrix[r][c] == target) {
count++;
}
}
}
return count;
}
matrix.length gives the number of rows. For non-square matrices the column count is matrix[r].length. Using row count for columns causes ArrayIndexOutOfBoundsException on wider matrices.int[] arr = {8, 3, 5, 3, 8}; Which statements are TRUE?-
arr.lengthreturns5 -
arr[arr.length - 1]returns8 -
arr[5]returns0because it is beyond the last initialized element
ArrayIndexOutOfBoundsException — does NOT return 0.Finished? Click to grade your exam and see explanations.
Your Results
0 / 42Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]