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

AP CSA Java Midterm – Units 1–2 (Primitive Types & Using Objects)

This AP Computer Science A Unit 1 and Unit 2 midterm focuses on primitive types, expressions, casting, String methods, the Math class, and using objects, aligned with the AP CSA Course and Exam Description and a typical CodeHS AP CSA course.

Select the best answer for each question, then click Submit Midterm at the bottom to see your score and explanations.

Question 1.
What does the following code print?

int result = 7 / 2;
System.out.println(result);
  



Question 2.
What does the following code print?

int x = 10;
int y = 3;
int z = x % y + y * 2;
System.out.println(z);
  



Question 3.
Which of the following is a valid variable declaration in Java?




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




Question 5.
What does this code print?

double x = 9.7;
int y = (int) x;
System.out.println(y);
  



Question 6.
What is the value of result after this code executes?

int result = 4 + 3 * 2;
  



Question 7.
Which of the following correctly updates x by adding 5 to its current value?




Question 8.
What is the value of the boolean expression (5 < 10) && (3 == 4)?




Question 9.
What does the following code print?

String s = "APCSA";
System.out.println(s.length());
  



Question 10.
What does this code print?

String word = "Computer";
System.out.println(word.substring(1, 4));
  



Question 11.
What is printed by the following code?

String text = "hello world";
System.out.println(text.indexOf("o"));
  



Question 12.
Which expression correctly checks whether two String variables s1 and s2 have the same contents?




Question 13.
Which Math method returns the square root of a number?




Question 14.
What does the following code print?

System.out.println(Math.pow(2, 3));
  



Question 15.
Which expression generates a random integer from 1 to 6 (inclusive)?




Question 16.
Which line correctly declares a constant named MAX_SCORE with value 100?




Question 17.
In Java, variables of type String are best described as:




Question 18.
What happens if you try to use a local variable in a method before giving it a value?




Question 19.
Consider this method header:

public double average(int a, int b)
  

Which statement is true?




Question 20.
Suppose there is a class Dog with a constructor Dog(String n, int a). Which of the following correctly creates a new Dog object?




Question 21.
Which of the following best describes what a class is in Java?




Question 22.
Consider the following method in a class:

public int getScore() {
    return score;
}
  

This method is best described as:




Question 23.
What does this code print?

System.out.println("AP" + 1 + 2);
  



Question 24.
What does this code print?

System.out.println(1 + 2 + "AP");
  



Question 25.
What is the value of result after this code executes?

int a = 2;
int b = 3;
double result = a + b / 2;
  



Question 26.
What does this code print?

int x = 5;
x++;
System.out.println(x);
  



Question 27.
What is the value of the boolean expression (3 > 1) || (4 < 2)?




Question 28.
What does the following code print?

int x = 2;
x *= 3 + 1;
System.out.println(x);
  



Question 29.
Which statement about short-circuit evaluation in Java is true?




Question 30.
What does this code print?

String str = "ap csa";
String result = str.toUpperCase().replace(" ", "");
System.out.println(result);
  



Question 31.
What is the result of "cat".compareTo("dog")?




Question 32.
Consider:

String s = "Java";
System.out.println(s.substring(0, 2));
  

What is printed?




Question 33.
Which statement about instance variables (fields) is true?




Question 34.
What happens when you declare:

String name = null;
System.out.println(name.length());
  



Question 35.
Given the method:

public static void addOne(int x) {
    x = x + 1;
}
  

What happens to the value of the variable passed into addOne from main?




Question 36.
Given:

public class Student {
    private String name;
    
    public Student(String n) {
        name = n;
    }
    
    public void setName(String n) {
        name = n;
    }
}
  

Which is true about setName?




Question 37.
What does this code print?

String s1 = "AP";
String s2 = s1;
s1 = s1 + "CSA";
System.out.println(s2);
  



Question 38.
What is printed by the following code?

int a = 10;
int b = 4;
System.out.println(a % b);
  



Question 39.
What does this code print?

double d = 5.6;
System.out.println(Math.round(d));
  



Question 40.
Which of the following correctly creates a String that is an uppercase version of message?

String message = "hello";
  



 

Contact form