CodeHS - AP CSA Midterm (Unit 1 & 2) - Questions and Solutions

AP CSA Cortado Midterm – Units 1–2

40-question multiple-choice practice for the CodeHS AP CSA Cortado curriculum, covering Unit 1: Using Objects and Methods and Unit 2: Selection and Iteration for the 2025 AP Computer Science A course.

Unit 1: Using Objects & Methods Unit 2: Selection & Iteration Topics: Strings, Math, APIs, Boolean Logic, Loops

This midterm-style exam mirrors the Cortado topic sequence: variables and data types, expressions, casting, String methods, the Math class, APIs & documentation, method calls, and object references (Unit 1), plus boolean expressions, if / else, nested selection, while and for loops, basic string algorithms, and informal run-time reasoning (Unit 2).

Select the best answer for each question, then click Submit Midterm to see your score, an estimated AP score, and detailed explanations.

Question 1.
What does the following code print?

int x = 5;
double y = 2;
double z = x / y;
System.out.println(z);
      

Question 2.
What is printed by this code?

int a = 3;
int b = 4;
a += 2 * b - 1;
System.out.println(a);
      

Question 3.
Which of the following is a valid Java variable declaration that follows standard naming conventions?

Question 4.
Which of the following is not a primitive type in Java?

Question 5.
What does this code print?

double d = 4.9;
int n = (int) (d + 0.5);
System.out.println(n);
      

Question 6.
Which line correctly uses the Math class to get the absolute value of -7?

Question 7.
Given the method header public static int max(int a, int b), what is the return type?

Question 8.
What does this code print?

System.out.println(Math.max(4, 9) - Math.min(4, 9));
      

Question 9.
Which line correctly creates a Scanner to read from the keyboard?

Question 10.
What does this code print?

String s = "Cortado";
System.out.println(s.charAt(s.length() - 1));
      

Question 11.
What is printed by the following code?

String code = "AP CSA";
System.out.println(code.indexOf("CS"));
      

Question 12.
What is the sign of the result of "beta".compareTo("alpha")?

Question 13.
What value does this expression evaluate to?

Integer.parseInt("42") + 1
      

Question 14.
Which expression generates a random int from 0 to 4 (inclusive)?

Question 15.
Which of the following is a proper Javadoc comment for a method that computes a score?

Question 16.
What does this code print?

Point p1 = new Point(1, 2);
Point p2 = p1;
p1.translate(1, 1);
System.out.println(p2);
      

(Assume Point has a standard toString that shows its coordinates.)

Question 17.
Which list of steps is a correct algorithm (in plain English) for computing the average of two test scores?

Question 18.
Given the method:

public static double average(int a, int b) {
    return (a + b) / 2.0;
}
      

What does System.out.println(average(4, 10)); print?

Question 19.
What does this code print?

String msg = "code";
msg.toUpperCase();
System.out.println(msg);
      

Question 20.
Which statement about the int type in Java is true?

Question 21.
What is the value of the boolean expression (5 < 8 && 2 * 3 == 6)?

Question 22.
What does this code print?

int score = 85;
if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("C");
}
      

Question 23.
What is printed by this nested if statement?

int x = 5;
int y = 10;
if (x < y) {
    if (x % 2 == 1) {
        System.out.println("odd");
    } else {
        System.out.println("even");
    }
} else {
    System.out.println("none");
}
      

Question 24.
What does this loop print?

int count = 0;
int n = 3;
while (n > 0) {
    count += n;
    n--;
}
System.out.println(count);
      

Question 25.
Which statement about while and for loops is generally true?

Question 26.
What does this loop print?

int total = 0;
for (int i = 1; i <= 4; i++) {
    total += i * i;
}
System.out.println(total);
      

Question 27.
Assume values is an array with 5 elements. What is wrong with this loop?

for (int i = 0; i <= values.length; i++) {
    System.out.println(values[i]);
}
      

Question 28.
What does this code print?

String s = "Cortado";
int vowelCount = 0;
for (int i = 0; i < s.length(); i++) {
    char ch = Character.toLowerCase(s.charAt(i));
    if (ch == 'a' || ch == 'e' || ch == 'i'
            || ch == 'o' || ch == 'u') {
        vowelCount++;
    }
}
System.out.println(vowelCount);
      

Question 29.
What happens when this code runs?

int x = 0;
if (x != 0 && 10 / x > 1) {
    System.out.println("Big");
}
      

Question 30.
Which pair of boolean expressions are logically equivalent (for any int x)?

Question 31.
What does this loop print?

for (int i = 1; i <= 5; i += 2) {
    System.out.print(i + " ");
}
      

Question 32.
How many times does the word Hi get printed?

for (int row = 0; row < 3; row++) {
    for (int col = 0; col < 2; col++) {
        System.out.println("Hi");
    }
}
      

Question 33.
With respect to a positive integer n, which fragment executes its inner statement about times?

Question 34.
What happens when this loop runs?

int n = 10;
while (n > 0) {
    System.out.println(n);
}
      

Question 35.
What does this loop print?

int i = 1;
int product = 1;
while (i <= 3) {
    product *= i;
    i++;
}
System.out.println(product);
      

Question 36.
Assume the array nums is defined as: int[] nums = {4, 9, 2, 7}; What does this code print?

int max = nums[0];
for (int i = 1; i < nums.length; i++) {
    if (nums[i] > max) {
        max = nums[i];
    }
}
System.out.println(max);
      

Question 37.
What does this code print?

String s = "cortado";
for (int i = 0; i < s.length(); i++) {
    if (i % 2 == 0) {
        System.out.print(s.charAt(i));
    }
}
      

Question 38.
What is the value of flag after this code runs?

int x = 7;
boolean flag;
if (x > 0 && x % 2 == 1) {
    flag = true;
} else {
    flag = false;
}
System.out.println(flag);
      

Question 39.
What is printed?

int sum = 0;
for (int i = 2; i <= 10; i += 2) {
    sum += i;
}
System.out.println(sum);
      

Question 40.
Which of the following best describes short-circuit evaluation in Java?

Contact form