Welcome to the Unit 2 Cumulative Mock Exam. This test combines everything you have learned across all Unit 2 topics: String manipulation, the Math class, object references, aliasing, null pointers, and wrapper classes.
On the real AP exam, questions are randomized and test your ability to spot subtle traps under time pressure. Use this 15-question challenge to verify your true exam readiness!
Unit 2 Mock Exam
Question 1 of 15 | Score: 0
1. What is printed as a result of executing the following code segment?
String s = “computer”;
System.out.println(s.substring(3, 6));
System.out.println(s.substring(3, 6));
Correct Answer: A
The
substring(from, to) method extracts from index 3 (inclusive) to index 6 (exclusive). Indices: c(0), o(1), m(2), p(3), u(4), t(5), e(6), r(7). Result is “put”.2. What is output by the following code?
String word = “hello”;
word.toUpperCase();
System.out.println(word);
word.toUpperCase();
System.out.println(word);
Correct Answer: B
String immutability!
word.toUpperCase() creates a new uppercase string object, but because it is not reassigned back to word, the original variable remains “hello”.3. Which of the following code segments simulates generating a random integer from 10 to 20, inclusive?
Correct Answer: B
Using the formula
(int)(Math.random() * ((max - min) + 1)) + min: (20 - 10) + 1 = 11 possible numbers, shifted by + 10.4. Consider the following code segment:
String s1 = new String(“test”);
String s2 = new String(“test”);
System.out.println((s1 == s2) + ” ” + s1.equals(s2));
String s2 = new String(“test”);
System.out.println((s1 == s2) + ” ” + s1.equals(s2));
What is printed?
Correct Answer: C
== compares memory addresses (different objects due to new, so false). .equals() compares character sequences (identical content, so true).5. What is output by the following code segment?
Integer a = 100;
Integer b = 100;
System.out.println(a == b);
Integer b = 100;
System.out.println(a == b);
Correct Answer: A
Because 100 falls within Java’s small integer cache range (-128 to 127), autoboxing references the same cached object, making
== evaluate to true.6. Consider the following code segment:
Car car1 = new Car(“Honda”);
Car car2 = car1;
car1.setModel(“Toyota”);
System.out.println(car2.getModel());
Car car2 = car1;
car1.setModel(“Toyota”);
System.out.println(car2.getModel());
What is printed?
Correct Answer: B
This is an aliasing example.
car1 and car2 point to the same object in memory, so changes made via car1 are visible through car2.7. What is output by the following code segment?
System.out.println(Math.pow(2, 3));
Correct Answer: B
Math.pow() always returns a double, so it prints 8.0.8. What happens when a program executes the following code?
String str = null;
System.out.println(str.indexOf(“a”));
System.out.println(str.indexOf(“a”));
Correct Answer: C
Calling any method on a reference variable holding
null causes a runtime NullPointerException.9. What is output by the following code segment?
int a = 4;
int b = 6;
System.out.println(“Result: ” + a + b);
int b = 6;
System.out.println(“Result: ” + a + b);
Correct Answer: B
Evaluated left to right:
"Result: " + 4 becomes "Result: 4", and concatenating 6 gives "Result: 46".10. Consider the following code segment:
Integer count = null;
int total = count + 5;
System.out.println(total);
int total = count + 5;
System.out.println(total);
What occurs during execution?
Correct Answer: C
Unboxing a
null Integer object during arithmetic operations triggers a runtime NullPointerException.11. What is printed as a result of executing the following code segment?
String s1 = “cat”;
String s2 = “dog”;
System.out.println(s1.compareTo(s2) < 0);
String s2 = “dog”;
System.out.println(s1.compareTo(s2) < 0);
Correct Answer: A
‘c’ comes before ‘d’ alphabetically, so
s1.compareTo(s2) returns a negative number, which is less than 0 (`true`).12. Which of the following correctly describes the behavior of
Math.abs()?
Correct Answer: B
Math.abs() is overloaded in Java; passing an int returns an int, and passing a double returns a double.13. What is the length of the string represented by the following literal?
String text = “A\\B\”C”;
System.out.println(text.length());
System.out.println(text.length());
Correct Answer: C
Escape sequences count as 1 character each. Breakdown: ‘A'(1), ‘\\'(2), ‘B'(3), ‘\”‘(4), ‘C'(5) -> wait: ‘A’, ‘\\’ (backslash), ‘B’, ‘\”‘ (quote). Total length = 4 characters.
14. Consider the following code segment:
Integer x = 50;
Integer y = 50;
System.out.println(x.equals(y));
Integer y = 50;
System.out.println(x.equals(y));
What is printed?
Correct Answer: A
The
.equals() method compares the underlying numeric values of wrapper objects, returning true.15. Which of the following correctly creates an empty string or extracts a substring to get the last character of a string
str?
Correct Answer: B
To get the last character up to the end of the string, you start at index
str.length() - 1 with a single argument substring(from).Unit 2 Mock Exam Complete!
0 / 15
Want to Ace the Entire AP CSA Exam?
Don’t leave your AP score to chance. Get expert guidance, custom FRQ breakdowns, and targeted 1-on-1 coaching with a Senior Java Engineer.
AP CSA 1-on-1 Tutoring