Practice a College Board-style free response question using String and Math methods. Write your Java, then reveal the model answer to see exactly what earns each point.
Free Response Question · Unit 1 · Using Objects and Methods
A programmer is working with a String variable and the Math class. Assume the following declarations:
String word = "Programming"; double value = -7.5;
Answer each part using appropriate Java expressions and method calls. (No loops or if-statements are needed — this unit is about objects and methods.)
A
Write a Java expression that evaluates to the number of characters in word, and state its value.
✓ Model answer
The expression is word.length(), which returns 11 (the String "Programming" has 11 characters).
Why it scores: Calls the length() instance method on the String with the dot operator and correctly reports 11. Writing length as a field (word.length) instead of a method call would lose the point.
B
Write a Java expression using a String method that returns the substring "gram" from word. Explain the indices you used.
✓ Model answer
word.substring(3, 7). In "Programming", index 0 is 'P', so 'g' is at index 3, and substring returns characters from index 3 up to but not including index 7 (indices 3, 4, 5, 6) — giving "gram".
Why it scores: Uses substring(from, to) with correct 0-based indices and explains that the second index is exclusive. Off-by-one indices (e.g., substring(3, 6)) would lose credit.
C
Write a Java expression using a Math method that returns the absolute value of value, and one that raises 2 to the power 5.
✓ Model answer
Absolute value: Math.abs(value), which returns 7.5. Power: Math.pow(2, 5), which returns 32.0 (a double).
Why it scores: Calls the static Math methods with the class name and dot operator and correct arguments, and knows Math.pow returns a double. Forgetting the class name (abs(value)) or swapping the pow arguments would lose points.
How to score points on AP CSA free response
Call methods with the dot operator — object.method() for instance methods, Class.method() for static ones.
Use exact method signatures from the Java Quick Reference (parameter order and types matter).
Remember 0-based indices and that substring’s second argument is exclusive.
Know return types — length() returns an int, Math.pow returns a double.
Answer the verb. "Write an expression" wants Java code; "explain" wants reasoning.