AP CSA Unit 1 Day 28: Comprehensive Final Review

Unit 1 Advanced (Cycle 2) Day 28 of 28 Advanced

Comprehensive Unit 1 Final Review

Section Mixed — Review: All Unit 1

Key Concept

This final review covers every testable Unit 1 concept at advanced difficulty. On the AP exam, approximately 17-22% of questions come from Unit 1 topics. The most commonly tested areas are: integer division versus floating-point division, casting behavior and placement, String method return values (especially substring() boundaries), the difference between == and equals(), Math.random() range formulas, and autoboxing/unboxing with wrapper classes. Mastering these foundations makes the remaining units significantly easier.

A method is intended to extract the initials from a full name. Consider the following implementation.

public static String getInitials(String fullName) { int space = fullName.indexOf(" "); String first = fullName.substring(0, 1); String last = fullName.substring(space, space + 1); return first + last; }

The call getInitials("John Smith") returns an unexpected result. What is wrong?

Answer: (B) The variable last gets the space character instead of "S".

Trace with "John Smith":

J(0) o(1) h(2) n(3) ␣(4) S(5) m(6) i(7) t(8) h(9)

space: indexOf(" ") = 4.

first: substring(0, 1) = "J". Correct.

last: substring(4, 5) = " " (the space character). Not "S"!

The fix: fullName.substring(space + 1, space + 2) to skip past the space and get "S".

Why Not the Others?

(A) The method returns "J " (J followed by a space), not "JS". The off-by-one error gets the space instead of the first letter of the last name.

(C) The string "John Smith" clearly contains a space at index 4. indexOf successfully finds it.

(D) substring(4, 5) is within bounds (the string has 10 characters). No exception is thrown; the result is simply wrong.

Common Mistake

When splitting a string at a delimiter, remember that indexOf gives the position of the delimiter itself. To get the character after the delimiter, you must add 1: substring(space + 1, space + 2).

AP Exam Tip

Error analysis questions are common on the AP exam. You must trace the code with specific values and compare the actual result to the intended result. The error is often an off-by-one mistake, not a crash or compile error.

Review this topic: Section Mixed — Review: All Unit 1 • Unit 1 Study Guide

More Practice

Back to blog

Leave a comment

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