AP CSA 2.10 Exercise 2: Implementing String Algorithms Applied Practice

Unit 2: Selection and Iteration · Lesson 2.10 · Exercise 2

Implementing String Algorithms: Applied Practice

Six scenarios on traversing text, and the fact that a String cannot be changed no matter what a method appears to do to it.

How this one is different

The exercise-1 editor asks you to process Strings. These six ask what substring actually returns and why an unassigned String method call accomplishes nothing.

Applied Practice

6 questions · scenario driven · every answer is recorded for your teacher

Question 1 of 6Analyze
A program calls s.toUpperCase(); on its own line and then prints s.
What is printed?
Correct. Correct. Every String method returns a new object and never modifies the receiver.
Incorrect. Nothing empties the original.
Incorrect. Discarding a return value is legal.
Incorrect. That requires assigning the result back to s.
Question 2 of 6Apply
Given String s = "computer", evaluate s.substring(2, 5).
What is the result?
Incorrect. That would be substring(2, 6), treating the end as inclusive.
Incorrect. That starts at index 1 and takes three characters.
Incorrect. That starts at index 1 rather than 2.
Correct. Correct: begins at index 2 inclusive, ends at index 5 exclusive, giving indices 2, 3 and 4.
Question 3 of 6Analyze
A loop traverses a String with for (int i = 0; i <= s.length(); i++) and calls s.charAt(i).
What happens?
Incorrect. The bounds are checked at run time.
Incorrect. It runs one pass too many.
Correct. Correct. Same off-by-one as arrays, with a String-specific exception name.
Incorrect. Every character is visited and then one more position that does not exist.
Question 4 of 6Apply
A method must report whether a String contains a given character.
What does indexOf return when the character is absent?
Incorrect. 0 is a valid position, meaning found at the very start.
Correct. Correct, and confusing 0 with "not found" is the classic error here.
Incorrect. It returns an int, which is never null.
Incorrect. Length is not used as a sentinel.
Question 5 of 6Evaluate
A loop builds a result by repeatedly doing result = result + ch; over a long String.
What is the hidden cost?
Correct. Correct, and it is why StringBuilder exists, though the pattern is still fine for short strings.
Incorrect. The output is right; only the cost is a problem.
Incorrect. It is ordinary legal code.
Incorrect. Immutability means every step allocates.
Question 6 of 6Apply
A method must compare two Strings for identical contents.
Which comparison is correct?
Incorrect. Relational operators do not apply to objects.
Incorrect. substring takes indices, not a String, and does not compare.
Incorrect. Compares references, so it can be false for equal contents.
Correct. Correct. compareTo serves when an ORDERING rather than equality is needed.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]