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

AP CSA Practice Exam #2 — Test Mode 1:30:00 0 / 42 answered
Study Mode — AP CSA Exam #2 Checked: 0 / 42 Correct: 0 Incorrect: 0
Answered: 0 / 42
Unit 1: Primitive Types, Objects & Methods (Q1-10)
Question 1Error Spotting
A student writes the following to compute the average of two integers. Which statement BEST describes the bug?
int a = 7;
int b = 2;
double avg = a / b;
System.out.println(avg);
Correct Answer: B. Integer division happens before assignment. 7 / 2 evaluates to 3 (int), then widens to 3.0. Fix: (double) a / b or a / (double) b.
Question 2Output Trace
What is printed?
String word = "Computer";
System.out.println(word.substring(0, 4).toUpperCase());
Correct Answer: A. substring(0,4) extracts indices 0-3: "Comp". toUpperCase() gives "COMP". The end index is exclusive.
Question 3Output Trace
What value is stored in result?
int x = 17;
int result = (x / 5) * 5 + (x % 5);
Correct Answer: B. x/5 = 3; 3*5 = 15; x%5 = 2; 15+2 = 17. This expression always reconstructs the original integer value.
Question 4I / II / III
Given: double p = -4.2; double q = 1.8; Which of the following evaluate to a value greater than 4?
  1. Math.abs(p)
  2. Math.pow(q, 3)
  3. Math.sqrt(25.0)
Correct Answer: D. I: |-4.2| = 4.2 > 4. II: 1.8^3 = 5.832 > 4. III: sqrt(25) = 5.0 > 4. All three exceed 4.
Question 5Error Spotting
The code below causes a runtime error. What is the cause?
String name = null;
int len = name.length();
System.out.println(len);
Correct Answer: B. Assigning null to a reference variable compiles fine. Calling any instance method on null throws a NullPointerException at runtime.
Question 6Output Trace
What is printed?
int m = 3;
int n = 4;
System.out.println("Sum: " + m + n);
System.out.println("Sum: " + (m + n));
Correct Answer: A. Left-to-right: "Sum: " + 3 = "Sum: 3", then + 4 = "Sum: 34". Parentheses in line 2 force arithmetic first: (3+4)=7, giving "Sum: 7".
Question 7Output Trace
What value is stored in z?
double d = 9.99;
int z = (int)(d * 10) % 7;
Correct Answer: A. d*10 = 99.9. Cast truncates to 99. 99 % 7 = 1 (since 14x7=98, 99-98=1).
Question 8I / II / III
After this executes, which statements are TRUE?
int alpha = 10;
int beta = 3;
alpha -= beta;
beta *= alpha;
alpha += beta;
  1. alpha equals 28
  2. beta equals 21
  3. alpha + beta equals 49
Correct Answer: D. alpha-=3 gives 7. beta*=7 gives 21. alpha+=21 gives 28. All three statements are true: alpha=28, beta=21, sum=49.
Question 9Concept
A program must generate a random integer from 5 to 12 inclusive. Which expression is correct?
Correct Answer: A. Range = 12-5+1 = 8. Formula: (int)(Math.random() * range) + min. B gives [5,11]; C gives [5,16]; D gives [4,11].
Question 10Error Spotting
This method is supposed to return twice the absolute value of its parameter. What is wrong?
public static int doubleAbs(int val) {
    if (val < 0) {
        val = -val;
    }
    val = val * 2;
}
Correct Answer: B. The return type is int but no return statement exists — compile error. Reassigning parameters is perfectly legal in Java.
Unit 2: Selection and Iteration (Q11-21)
Question 11Error Spotting
A student writes the following to assign a letter grade. What actually prints for 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);
Correct Answer: B. Each 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.
Question 12I / II / III
Which are equivalent to !(x > 5 && y <= 10)?
  1. x <= 5 || y > 10
  2. !(x > 5) || !(y <= 10)
  3. x <= 5 && y > 10
Correct Answer: B. De Morgan's Law: !(A && B) = !A || !B. I and II are both equivalent. III uses && instead of || — not equivalent.
Question 13Output Trace
What is printed?
int ctr = 1, total = 0;
while (ctr <= 5) {
    if (ctr % 2 != 0) {
        total += ctr;
    }
    ctr++;
}
System.out.println(total);
Correct Answer: B. Adds odd numbers 1-5: 1+3+5 = 9.
Question 14Error Spotting
Which loop header has an off-by-one error when intending to iterate over integers 1 through n inclusive?
Correct Answer: C. Option C stops at i = n-1, missing value n. Options A, B, and D all correctly cover 1 through n.
Question 15Output Trace
What is printed?
int count = 0;
for (int r = 0; r < 4; r++) {
    for (int c = r; c < 4; c++) {
        count++;
    }
}
System.out.println(count);
Correct Answer: C. r=0: 4 iterations. r=1: 3. r=2: 2. r=3: 1. Total = 4+3+2+1 = 10.
Question 16I / II / III
This for loop computes 6! (720). Which while loops produce the same result?
int product = 1;
for (int k = 1; k <= 6; k++) { product *= k; }
  1. int k = 1, product = 1;
    while (k <= 6) { product *= k; k++; }
  2. int k = 1, product = 1;
    while (k < 7) { k++; product *= k; }
  3. int k = 0, product = 1;
    while (k < 6) { k++; product *= k; }
Correct Answer: C. I multiplies k=1 through 6 in order — correct. II increments before multiplying so it computes 2x3x4x5x6x7 — wrong. III starts k=0, increments to 1 first, then multiplies: 1x2x3x4x5x6 — correct.
Question 17Output Trace
What does 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;
}
Correct Answer: A. i=4 picks 'e'; i=2 picks 'c'; i=0 picks 'a'; i=-2 stops. result = "eca".
Question 18I / II / III
getValue() has a side effect (it prints "called"). With x = 0, this runs:
boolean r = (x != 0) && (getValue() > 0);
  1. r is false
  2. getValue() is NOT called due to short-circuit evaluation
  3. "called" is printed to the console
Correct Answer: B. 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).
Question 19Error Spotting
Which loop runs indefinitely?
Correct Answer: A. n: 10, 7, 4, 1, -2, -5... It skips over 0, so n != 0 is always true. Option D terminates correctly at n=0.
Question 20Output Trace
What is printed?
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");
}
Correct Answer: B. p=6>5 true, enter outer if. q=4>5 false, take else branch: prints B.
Question 21Output Trace
What is printed?
int n = 16;
int count = 0;
while (n > 1) {
    n = n / 2;
    count++;
}
System.out.println(count);
Correct Answer: B. 16→8→4→2→1. Loop runs 4 times before n=1 fails the condition. This computes log base 2 of 16.
Unit 3: Class Creation (Q22-32)
Question 22Error Spotting
What is wrong with this constructor?
public class Thermometer {
    private double temperature;

    public Thermometer(double temperature) {
        temperature = temperature;
    }
}
Correct Answer: B. The parameter shadows the instance variable. The assignment sets the parameter to itself. Fix: this.temperature = temperature;
Question 23I / II / III
Given: Counter c1 = new Counter(); Counter c2 = c1; c1.increment(); c1.increment(); Which statements are TRUE?
  1. c1.getCount() returns 2
  2. c2.getCount() returns 0
  3. c1 and c2 refer to the same object in memory
Correct Answer: B. 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).
Question 24Concept
Which is the BEST reason to declare instance variables private?
Correct Answer: B. Encapsulation hides data behind controlled methods. A, C, and D are false statements about access modifiers in Java.
Question 25Error Spotting
What error will this method cause?
public class BankAccount {
    private double balance;

    public boolean isRich(double threshold) {
        if (balance > threshold) {
            return true;
        } else if (balance < threshold) {
            return false;
        }
    }
}
Correct Answer: B. When 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.
Question 26Output Trace
The Dog class overrides toString() to return name + " (" + age + ")". What is printed?
Dog d = new Dog("Rex", 3);
System.out.println(d);
Correct Answer: B. println(obj) calls toString() automatically. Since it's overridden, output is Rex (3). Without the override, C would occur.
Question 27I / II / III
Rectangle r = new Rectangle(4, 6) with private fields width=4, height=6. Which statements are TRUE?
  1. r.area() returns 24
  2. r.perimeter() returns 20
  3. r.width can be accessed directly from client code
Correct Answer: B. I: 4x6=24. II: 2x(4+6)=20. III: width is private — direct access from client code causes a compile error.
Question 28Concept
A setGPA(double g) setter should reject values outside [0.0, 4.0]. Which implementation is CORRECT?
Correct Answer: B. A uses strict inequalities, excluding 0.0 and 4.0. C and D use || which is always true for any finite number. Only B correctly accepts the closed interval [0.0, 4.0].
Question 29Error Spotting
Which statement about this class is TRUE?
public class Coin {
    private String side = "heads";

    public void flip() {
        side = (Math.random() < 0.5) ? "tails" : "heads";
    }

    public static String getSide() {
        return side;
    }
}
Correct Answer: B. A static method cannot access non-static (instance) fields. Fix: remove static from getSide().
Question 30Output Trace
What is printed?
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));
Correct Answer: C. 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.
Question 31I / II / III
Constructor: public Car(String make, int year). Which create a valid Car object?
  1. Car c = new Car("Toyota", 2024);
  2. Car c = new Car(2024, "Toyota");
  3. Car c = new Car("Honda", 1999);
Correct Answer: C. Constructor requires (String, int). I: ("Toyota",2024) valid. II: (2024,"Toyota") reverses types — compile error. III: ("Honda",1999) valid.
Question 32Error Spotting
A student compares two String objects. What is wrong?
String s1 = new String("hello");
String s2 = new String("hello");
if (s1 == s2) {
    System.out.println("equal");
} else {
    System.out.println("not equal");
}
Correct Answer: B. == compares references. new String("hello") creates two separate heap objects, so == is false. Use .equals() to compare content.
Unit 4: Data Collections — Arrays & ArrayLists (Q33-42)
Question 33Error Spotting
What error occurs when this runs?
int[] nums = {10, 20, 30, 40, 50};
for (int i = 0; i <= nums.length; i++) {
    System.out.println(nums[i]);
}
Correct Answer: B. i <= nums.length allows i=5. Valid indices are 0-4, so nums[5] throws ArrayIndexOutOfBoundsException. Fix: i < nums.length.
Question 34Output Trace
What is printed?
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);
Correct Answer: C. Tracks maximum: 3→4→5→9. Maximum in array is 9.
Question 35Error Spotting
A student tries to double every element. Why does this NOT work?
int[] scores = {70, 80, 90};
for (int s : scores) {
    s = s * 2;
}
System.out.println(scores[0]);
Correct Answer: B. Enhanced for creates a local primitive copy. Modifying it doesn't affect the array. Fix: use an indexed loop for (int i=0; i
Question 36I / II / III
ArrayList words starts as ["cat","dog","bird"]. After words.add(1,"fish"); then words.remove(2); which are TRUE?
  1. words.size() returns 3
  2. words.get(0) returns "cat"
  3. words.get(1) returns "dog"
Correct Answer: B. Start: ["cat","dog","bird"]. After add(1,"fish"): ["cat","fish","dog","bird"]. After remove(2): ["cat","fish","bird"]. Size=3 (I true), get(0)="cat" (II true), get(1)="fish" not "dog" (III false).
Question 37Error Spotting
A student removes all even numbers from an ArrayList. What is the bug?
ArrayList nums = 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);
    }
}
Correct Answer: B. Removing element at index i shifts the next element to index i, but i then increments — skipping it. Fix: decrement i after removal, or iterate backward.
Question 38Output Trace
What is printed?
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);
Correct Answer: C. Sums the main diagonal: grid[0][0]=1, grid[1][1]=5, grid[2][2]=9. Total = 15.
Question 39I / II / III
Which correctly describe differences between arrays and ArrayLists?
  1. Arrays have a fixed size; ArrayLists can grow and shrink dynamically.
  2. Arrays can hold primitive types; ArrayLists require wrapper classes such as Integer.
  3. ArrayLists are faster than arrays when accessing elements by index.
Correct Answer: B. I and II are true AP exam distinctions. III is false — array index access is generally faster due to no boxing overhead.
Question 40Output Trace
What is the final content of the ArrayList?
ArrayList list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
list.add(0, 5);
list.set(2, 25);
list.remove(3);
Correct Answer: B. After 3 adds: [10,20,30]. add(0,5): [5,10,20,30]. set(2,25): [5,10,25,30]. remove(3): [5,10,25].
Question 41Error Spotting
This method counts elements equal to a target in a 2D array. What is the error?
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;
}
Correct Answer: B. 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.
Question 42I / II / III
Given: int[] arr = {8, 3, 5, 3, 8}; Which statements are TRUE?
  1. arr.length returns 5
  2. arr[arr.length - 1] returns 8
  3. arr[5] returns 0 because it is beyond the last initialized element
Correct Answer: B. I: arr.length=5 true. II: arr[4]=8 true. III: arr[5] throws ArrayIndexOutOfBoundsException — does NOT return 0.

Finished? Click to grade your exam and see explanations.

Your Results

0 / 42
AP Score: --
Unit 1 (Q1-10)
--
Unit 2 (Q11-21)
--
Unit 3 (Q22-32)
--
Unit 4 (Q33-42)
--

AP CSA FRQ Archive — Practice Free Response
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.