PrepGo

AP Computer Science A Practice Quiz: Implementing String Algorithms

Written by AP Content Team, Verified for 2026 AP Exams, Last updated: May 2026

Test your understanding with short quizzes. This quiz has 7 questions to check your progress.

Question 1 of 7

Consider the following method designed to reverse a given string. public String reverse(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { result = str.substring(i, i + 1) + result; } return result; } What is the value returned by the call reverse("APEX")?

All Questions (7)

Consider the following method designed to reverse a given string. public String reverse(String str) { String result = ""; for (int i = 0; i < str.length(); i++) { result = str.substring(i, i + 1) + result; } return result; } What is the value returned by the call reverse("APEX")?

A) APEX

B) XEPA

C) PEXA

D) A

Correct Answer: B

This is a standard algorithm to create a new string with characters reversed. The loop iterates through the original string `str` from left to right. In each iteration, it takes the current character and prepends it to the `result` string. Iteration 1: result = "A" + "" -> "A" Iteration 2: result = "P" + "A" -> "PA" Iteration 3: result = "E" + "PA" -> "EPA" Iteration 4: result = "X" + "EPA" -> "XEPA" The final returned value is "XEPA".

A programmer is developing an algorithm to determine the number of substrings that meet a specific criterion. The method `countSub` is intended to count all substrings of length 2 in `myString` that start with the letter 'a'. public int countSub(String myString) { int count = 0; // Missing loop return count; } Which of the following code segments could replace `// Missing loop` to correctly implement the algorithm?

A) for (int i = 0; i < myString.length(); i++) { if (myString.substring(i, i + 2).equals("a")) { count++; } }

B) for (int i = 0; i < myString.length() - 1; i++) { if (myString.substring(i, i + 1).equals("a")) { count++; } }

C) for (int i = 0; i < myString.length() - 1; i++) { if (myString.substring(i, i + 2).startsWith("a")) { count++; } }

D) for (int i = 0; i < myString.length(); i++) { if (myString.substring(i, i + 1).equals("a")) { count++; } }

Correct Answer: C

The algorithm needs to check substrings of length 2. The loop must stop at `myString.length() - 1` to avoid an `IndexOutOfBoundsException` when `i + 2` is calculated. Option C correctly sets the loop bounds and uses `substring(i, i + 2)` to get a two-character string, then checks if it starts with 'a'. Option A has incorrect loop bounds. Options B and D check substrings of length 1, not length 2.

Consider the following algorithm, which is intended to find if a substring has a particular property. The property is defined as a three-character substring where the first and last characters are the same. public boolean hasProperty(String text) { for (int j = 0; j < text.length() - 2; j++) { String first = text.substring(j, j + 1); String last = text.substring(j + 2, j + 3); if (first.equals(last)) { return true; } } return false; } What is the result of the call `hasProperty("abacaba")`?

A) true, because of the substring "aba" at index 0.

B) true, because of the substring "aca" at index 2.

C) false, because the loop terminates early.

D) An IndexOutOfBoundsException will be thrown.

Correct Answer: A

The algorithm iterates through the string to find a 3-character substring where the first and last characters match. - When j=0, the substring being considered is from index 0 to 2 ("aba"). The `first` character is "a" (from substring(0,1)) and the `last` is "a" (from substring(2,3)). Since `first.equals(last)` is true, the method immediately returns `true` and stops execution. It does not continue to check the rest of the string.

An original algorithm is implemented in the `buildNewString` method below. It creates a new string by taking characters from the input `source` string at even indices (0, 2, 4, ...). public String buildNewString(String source) { String newStr = ""; for (int k = 0; k < source.length(); k = k + 2) { newStr = newStr + source.substring(k, k + 1); } return newStr; } What is the output of `System.out.println(buildNewString("ALGORITHM"));`?

A) ALGORITHM

B) AGRTM

C) LORIH

D) AGRT

Correct Answer: B

This question requires determining the result of the given algorithm. The loop starts at k=0 and increments by 2 in each step. It appends the character at the current index `k` to `newStr`. - k=0: appends 'A' - k=2: appends 'G' - k=4: appends 'R' - k=6: appends 'T' - k=8: appends 'M' The loop terminates when k becomes 10, which is greater than the length of the string (9). The final string is "AGRTM".

An algorithm is needed to determine the number of times the substring "io" appears in a string, but only when it is immediately preceded by the character 't'. For example, in "triumph trio", the count would be 1 ("trio"), not 2. Consider the following implementation: int count = 0; for (int i = 0; i < str.length() - 2; i++) { String segment = str.substring(i, i + 3); /* condition */ { count++; } } Which boolean expression should replace `/* condition */` to correctly implement the algorithm?

A) if (segment.equals("tio"))

B) if (segment.substring(0, 1).equals("t") && segment.substring(1, 3).equals("io"))

C) if (segment.startsWith("t") && segment.endsWith("o"))

D) if (segment.indexOf("tio") != -1)

Correct Answer: A

The algorithm's goal is to count substrings that meet the specific criteria of being exactly "tio". The loop correctly extracts three-character segments. Therefore, the condition only needs to check if the extracted `segment` is equal to "tio". Option A is the most direct and correct way to do this. Option B is functionally equivalent but unnecessarily complex. Option C is incorrect because it would match strings like "tango" or "taco". Option D is incorrect because `indexOf` would always be 0 if the segment contains "tio", making it less direct than a simple equality check.

An algorithm is designed to find the number of non-overlapping occurrences of a specific substring (`target`) within a larger string (`text`). Consider the execution of this algorithm on the string `text = "ababababa"` with `target = "aba"`. What is the result?

A) 1

B) 2

C) 3

D) 4

Correct Answer: B

This question requires determining the result of a standard string algorithm. For non-overlapping occurrences, once a match is found, the search for the next match must begin after the found substring ends. 1. Search starts at index 0. A match for "aba" is found at index 0. 2. The search for the next occurrence resumes at index 3 (the position immediately after the first match). 3. Starting from index 3, the substring is "bababa". A match for "aba" is found at index 4 of the original string. 4. The search for the next occurrence resumes at index 7 (after the second match). 5. Starting from index 7, the substring is "ba". No more matches are found. The total number of non-overlapping occurrences is 2.

Which of the following code segments correctly implements a standard algorithm to create a new string with the characters of `original` in reversed order?

A) String reversed = ""; for (int i = 0; i < original.length(); i++) { reversed = reversed + original.substring(i, i + 1); } return reversed;

B) String reversed = ""; for (int i = original.length(); i > 0; i--) { reversed = reversed + original.substring(i, i + 1); } return reversed;

C) String reversed = ""; for (int i = original.length() - 1; i >= 0; i--) { reversed = reversed + original.substring(i, i + 1); } return reversed;

D) String reversed = original; for (int i = 0; i < original.length(); i++) { reversed.substring(i, i + 1) = original.substring(original.length() - 1 - i, original.length() - i); } return reversed;

Correct Answer: C

This question asks for the correct development of a standard string reversal algorithm. Option C correctly iterates backward from the last character of the string (index `length() - 1`) down to the first character (index 0). In each iteration, it appends the character at the current index `i` to the `reversed` string, effectively building the reversed version. Option A just copies the string. Option B starts its index out of bounds (`original.length()`) and would cause an error. Option D attempts to assign a value to a substring call, which is not valid Java syntax as strings are immutable.