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
All Questions (7)
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) 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.
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.
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".
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.
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.
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.