CodeHS - AP CSA Cortado Midterm (Difficulty Hard) - Questions and Solutions

AP CSA Java Midterm – Cortado Units 1 & 2 (Hard Version)

This 40-question AP Computer Science A midterm is aligned with the 2025 AP CSA Course and Exam Description and the Cortado Units 1 (Using Objects and Methods) and 2 (Selection and Iteration). Expect questions that require careful code tracing, boolean logic, String algorithms, and loop reasoning.

Focus: Using Objects & Methods (Unit 1) Focus: Selection & Iteration (Unit 2) Difficulty: Hard Question Type: Multiple Choice

Select the best answer for each question, then click Submit Midterm to see your score, estimated AP score, and explanations. This is designed to feel similar to a CodeHS Cortado style unit exam.

1.1 · Algorithms, Programming, Compilers

Question 1. Which statement best describes how Java programs are executed?

1.2 · Variables & Types

Question 2. Which declaration is illegal?

1.3 · Expressions & Output

Question 3. What does the following print?

System.out.println(2 + 3 * 4 - 8 / 2);
1.4 · Assignment & Input

Question 4. What is printed?

int x = 5;
int y = x;
y = y + 3;
System.out.println(x + " " + y);
1.5 · Casting & Range

Question 5. What is printed?

int n = 2000000000;
n = n + 2000000000;
System.out.println(n);
1.5 · Casting

Question 6. What is printed?

double d = 7 / 2;
double e = 7 / 2.0;
System.out.println(d + " " + e);
1.6 · Compound Assignment

Question 7. What is printed?

int x = 4;
x *= 2 + 3;
System.out.println(x);
1.7 · API & Libraries

Question 8. Which call is valid?

1.8 · Comments

Question 9. Which is valid Javadoc?

1.9 · Method Signatures

Question 10. Which pair demonstrates valid overloading?

1.10–1.11 · Math Methods

Question 11. What is printed?

double x = Math.pow(2, 3);
double y = Math.sqrt(81);
System.out.println((int)(y - x));
1.11 · Math.random

Question 12. Generates random integer −2 to 2?

1.12–1.13 · Objects & References

Question 13. What is printed?

Point a = new Point(1,2);
Point b = a;
b.translate(3,0);
System.out.println(a);
1.14–1.15 · Strings

Question 14. What prints?

String s="abca";
s=s.substring(1);
s=s.replace("a","xy");
System.out.println(s);
1.15 · compareTo

Question 15. What is the sign of the result of "Zoo".compareTo("apple")?

2.2 · Boolean Expressions

Question 16. What is the value of expr?

boolean expr = !(3 <= 4 && (2 != 2 || 5 > 1));
2.3–2.4 · if / nested if

Question 17. What is printed when x = 7?

public static void check(int x) {
    if (x > 0) {
        if (x % 2 == 0) {
            System.out.println("A");
        } else if (x > 5) {
            System.out.println("B");
        } else {
            System.out.println("C");
        }
    } else {
        System.out.println("D");
    }
}
2.5–2.6 · Boolean Equivalence

Question 18. Which expression is equivalent to !(x >= 10 || y == 0)?

2.7 · while loops

Question 19. How many times does the loop execute?

int count = 0;
int n = 1;
while (n < 100) {
    n *= 3;
    count++;
}
2.7 · While & Short-Circuit

Question 20. What happens?

int x = 0;
while (x != 0 && 10 / x > 1) {
    System.out.println("Loop");
}
2.8 · for loops

Question 21. What is printed?

int sum = 0;
for (int i = 1; i <= 5; i++) {
    sum += i * 2;
}
System.out.println(sum);
2.8 · for loops

Question 22. Which loop prints even numbers 2–10?

2.9 · Algorithms

Question 23. Counts odd digits. What prints?

int n = 12345;
int count = 0;
while (n > 0) {
    if (n % 2 == 1) count++;
    n /= 10;
}
System.out.println(count);
2.10 · String Algorithms

Question 24. What does this print?

String s = "ABABBA";
int runs = 1;
for (int i = 1; i < s.length(); i++) {
    if (s.charAt(i) != s.charAt(i-1)) {
        runs++;
    }
}
System.out.println(runs);
2.11 · Nested Iteration

Question 25. How many times is "*" printed?

for(int row=1;row<=3;row++){
  for(int col=1;col<=row;col++){
    System.out.println("*");
  }
}
2.11 · Nested Iteration

Question 26. What is printed?

int count = 0;
for(int i=1;i<=4;i++){
  for(int j=1;j<=3;j++){
    if((i*j)%2==0) count++;
  }
}
System.out.println(count);
2.12 · Runtime

Question 27. What is the time complexity?

for(int i=0;i

  
  
  
  
2.12 · Runtime

Question 28. What is complexity?

int i = 1;
while(i < n){
  i = i * 2;
}
1.14–2.10 · String Search

Question 29. What prints?

String s="APCSAPCS";
int count=0;
for(int i=0;i

  
  
  
  
1.5–1.6 · Increment

Question 30. What prints?

int x=3;
int y=x++ + ++x;
System.out.println(x+" "+y);
2.5 · Short-Circuit

Question 31. What prints?

int n = 0;
boolean b = n != 0 && 10/n > 1;
System.out.println(b);
1.15 · String Immutability

Question 32. What prints?

String a="java";
String b=a.toUpperCase();
a.replace("a","x");
System.out.println(a+" "+b);
1.4–1.8 · Scope

Question 33. What is the result?

public static void foo(){
  int x;
  if(true){ x=5; }
  System.out.println(x);
}
2.7 · Loop Behavior

Question 34. For which starting value does this loop run infinitely?

while(n > 0){
  if(n % 2 == 0) n /= 2;
  else n = n + 1;
}
1.12–1.14 · Reference Parameters

Question 35. What prints?

public static void move(Point p){ p.translate(1,1); }
Point a = new Point(0,0);
move(a);
System.out.println(a);
2.3–2.5 · Selection Logic

Question 36. What prints?

int x=4;
if(x>2 && x<5)
  System.out.print("A");
if(x<4 || x==4)
  System.out.print("B");
else
  System.out.print("C");
2.8–2.9 · Loop + Break

Question 37. What prints?

int total = 0;
for(int i=1;i<=10;i++){
  total += i;
  if(total > 15){
    break;
  }
}
System.out.println(total);
1.11 · Math

Question 38. What prints?

double v = -3.2;
System.out.println(Math.ceil(v) + " " + Math.floor(v));
2.10 · String Search

Question 39. What prints?

String s="coconut";
int index = s.indexOf("co", 1);
System.out.println(index);
1.10–1.15 · Mixed

Question 40. What is printed?

String s="1234";
int a=Integer.parseInt(s.substring(0,2));
int b=Integer.parseInt(s.substring(2));
System.out.println(Math.max(a,b));
Tip: You can see only missed questions after grading.

 

Contact form