String Manipulation
Help Questions
AP Computer Science A › String Manipulation
Password Validator: What is the result of the following code snippet when the input is "Z9xxxxxx"?
String p = input;
boolean hasUpper = p.toUpperCase().indexOf(p.substring(0, 1)) >= 0;
boolean hasDigit = p.indexOf("9") >= 0;
System.out.println(hasUpper && hasDigit && p.length() >= 8);
false
TRUE
false true
true
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods with boolean logic. String manipulation in Java involves checking string properties and combining boolean conditions, where understanding how indexOf() works with substrings is crucial. In this problem, for 'Z9xxxxxx': hasUpper checks if the uppercase version contains the first character 'Z', which is true since 'Z' is already uppercase. hasDigit checks if '9' exists, which is true. The length is 8, which satisfies >= 8. All three conditions (true && true && true) evaluate to true. Choice B is correct because all three conditions are satisfied: the string starts with an uppercase letter, contains a digit, and has exactly 8 characters. Choice A would result if any condition failed, C shows incorrect capitalization, and D shows multiple values which isn't possible for a single boolean expression. To help students: Break down compound conditions and evaluate each part separately. Understand that p.toUpperCase().indexOf(p.substring(0,1)) >= 0 cleverly checks if the first character is uppercase. Watch for: understanding that all conditions in an AND expression must be true.
Palindrome Checker: What will be the output if the string is "Level"?
String s = input.toUpperCase();
char first = s.charAt(0);
char last = s.charAt(s.length() - 1);
System.out.println(first == last);
True
"L==l"
false
true
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods like toUpperCase() and charAt(). String manipulation in Java involves accessing individual characters and comparing them, where understanding character comparison with == is valid for primitive char types. In this problem, 'Level' becomes 'LEVEL' after toUpperCase(), then charAt(0) gets 'L' and charAt(4) also gets 'L'. The comparison 'L' == 'L' evaluates to true. Choice A is correct because both the first and last characters of 'LEVEL' are 'L', making the comparison true. Choice B would be the result if the characters were different, C shows incorrect capitalization for boolean values in Java, and D shows a string literal instead of a boolean result. To help students: Remember that charAt() returns a primitive char, which can be compared with ==. Practice identifying first and last characters using proper indexing. Watch for: understanding that s.length() - 1 gives the index of the last character.
Palindrome Checker: What is the result of the following code snippet when the input is "noon"?
String s = input.toUpperCase();
String mid = s.substring(1, s.length() - 1);
mid = mid.replace("O", "A");
System.out.println(mid);
OO
AA
OA
O
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods like substring() and replace(). String manipulation in Java involves extracting portions of strings and replacing characters, where understanding substring indices is crucial. In this problem, 'noon' becomes 'NOON' after toUpperCase(). Then substring(1, 3) extracts characters from index 1 to 2 (excluding 3), giving 'OO'. The replace('O', 'A') changes both O's to A's, resulting in 'AA'. Choice B is correct because the middle substring 'OO' has all O's replaced with A's. Choice A would result from replacing only one O, C shows the original substring without replacement, and D shows only one character instead of two. To help students: Remember that substring(start, end) excludes the character at the end index. Practice extracting middle portions of strings and understand that replace() affects all occurrences. Watch for: calculating the correct indices for extracting the middle portion of a string.
Password Validator: What will be the output if the string is "Abcdefg9"?
public class PasswordCheck {
public static String check(String password) {
password = password.replace(" ", "");
String preview = password.substring(0, 2).toUpperCase();
boolean hasUpper = false;
boolean hasDigit = false;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c >= 'A' && c <= 'Z') hasUpper = true;
if (c >= '0' && c <= '9') hasDigit = true;
}
if (password.length() >= 8 && hasUpper && hasDigit) {
return "OK:" + preview;
}
return "INVALID:" + preview;
}
}
// Test case: System.out.println(PasswordCheck.check("Abcdefg9"));
INVALID:AB
OK:BC
OK:AB
OK:Ab
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods for password validation. String manipulation in Java requires understanding how methods like length(), charAt(), and substring() work together to analyze string properties. In this problem, the code validates "Abcdefg9" by removing spaces (none present), creating a preview "AB" from the first two characters uppercased, and checking for uppercase letters and digits. The password has 8 characters (meets minimum), contains uppercase 'A', and has digit '9'. Choice A is correct because all three conditions are satisfied: length >= 8, hasUpper is true (from 'A'), and hasDigit is true (from '9'), resulting in "OK:AB". Choice B would incorrectly suggest the password is invalid when it meets all requirements. To help students: Draw a table tracking each variable's value as you trace through the code. Remember that character comparisons like 'A' <= c <= 'Z' check ASCII values to identify character types.
Password Validator: What will be the output if the string is "A bcdef9"?
public class PasswordCheck {
public static String check(String password) {
password = password.replace(" ", "");
String preview = password.substring(0, 2).toUpperCase();
boolean hasUpper = false;
boolean hasDigit = false;
for (int i = 0; i < password.length(); i++) {
char c = password.charAt(i);
if (c >= 'A' && c <= 'Z') hasUpper = true;
if (c >= '0' && c <= '9') hasDigit = true;
}
if (password.length() >= 8 && hasUpper && hasDigit) {
return "OK:" + preview;
}
return "INVALID:" + preview;
}
}
// Test case: System.out.println(PasswordCheck.check("A bcdef9"));
OK:A
OK:AB
INVALID:AB
INVALID:A
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding how replace() affects strings before further processing. String manipulation often involves preprocessing steps like removing spaces, which can change string length and character positions. In this problem, "A bcdef9" first has its space removed to become "Abcdef9" (7 characters), then the preview "AB" is created from the first two characters uppercased. The password has uppercase 'A' and digit '9', but only 7 characters after space removal. Choice A is correct because while the password has uppercase and digit requirements met, it fails the length check (7 < 8), resulting in "INVALID:AB". Choice D incorrectly shows "A " as the preview, but spaces are removed before creating the preview. To help students: Always trace preprocessing steps first before analyzing the main logic. Watch for how string modifications affect subsequent operations, especially with indices and length checks.
Palindrome Checker: What will be the output if the string is "Level"?
public class PalTool {
public static String check(String word) {
word = word.replace(" ", "");
String lower = word.toLowerCase();
String ends = lower.substring(0, 1).toUpperCase() + lower.substring(lower.length() - 1);
boolean isPal = true;
for (int i = 0; i < lower.length() / 2; i++) {
if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) {
isPal = false;
}
}
return (isPal ? "PAL:" : "NOT:") + ends;
}
}
// Test case: System.out.println(PalTool.check("Level"));
NOT:Ll
PAL:Ll
PAL:Le
PAL:LL
Explanation
This question tests AP Computer Science A string manipulation, specifically palindrome checking with case-insensitive comparison. String manipulation for palindromes requires converting to consistent case and comparing characters from both ends. In this problem, "Level" becomes "level" after toLowerCase(), and the code checks if characters mirror each other: l==l, e==e, v==v (middle character). The ends string combines uppercase first character 'L' with lowercase last character 'l'. Choice A is correct because "level" is indeed a palindrome (reads the same forwards and backwards), resulting in "PAL:Ll". Choice D incorrectly shows both characters uppercase, while B has the wrong last character. To help students: Visualize palindrome checking by drawing arrows between matching positions. Practice with both even and odd-length strings to understand the loop boundary.
Palindrome Checker: What will be the output if the string is "Race car"?
public class PalTool {
public static String check(String word) {
word = word.replace(" ", "");
String lower = word.toLowerCase();
String ends = lower.substring(0, 1).toUpperCase() + lower.substring(lower.length() - 1);
boolean isPal = true;
for (int i = 0; i < lower.length() / 2; i++) {
if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) {
isPal = false;
}
}
return (isPal ? "PAL:" : "NOT:") + ends;
}
}
// Test case: System.out.println(PalTool.check("Race car"));
PAL:R
PAL:Rr
PAL:Ra
NOT:Rr
Explanation
This question tests AP Computer Science A string manipulation, specifically handling spaces in palindrome checking. String preprocessing like removing spaces is crucial for palindrome detection in real-world applications. In this problem, "Race car" first has its space removed to become "Racecar", then converts to "racecar" for checking. The palindrome check compares: r==r, a==a, c==c, e==e (middle), confirming it's a palindrome. The ends string uses uppercase 'R' and lowercase 'r'. Choice A is correct because "racecar" without spaces is a valid palindrome, resulting in "PAL:Rr". Choice C incorrectly suggests the space remains in the ends string, but spaces are removed before processing. To help students: Always trace preprocessing steps before main logic. Remember that palindrome checking often requires normalizing the input (removing spaces, converting case).
Palindrome Checker: What will be the output if the string is "Java"?
public class PalTool {
public static String check(String word) {
word = word.replace(" ", "");
String lower = word.toLowerCase();
String ends = lower.substring(0, 1).toUpperCase() + lower.substring(lower.length() - 1);
boolean isPal = true;
for (int i = 0; i < lower.length() / 2; i++) {
if (lower.charAt(i) != lower.charAt(lower.length() - 1 - i)) {
isPal = false;
}
}
return (isPal ? "PAL:" : "NOT:") + ends;
}
}
// Test case: System.out.println(PalTool.check("Java"));
NOT:Ja
PAL:Jv
NOT:Jv
PAL:Ja
Explanation
This question tests AP Computer Science A string manipulation, specifically palindrome checking with non-palindrome inputs. Understanding when palindrome checks fail helps students debug their own implementations. In this problem, "Java" becomes "java" in lowercase, and the palindrome check compares: j!=a and a!=v, immediately setting isPal to false. The ends string combines uppercase 'J' with lowercase 'a'. Choice B is correct because "java" is not a palindrome (j≠a when comparing first and last characters), resulting in "NOT:Ja". Choice C incorrectly shows 'v' as the last character when it should be 'a'. To help students: Test palindrome checkers with both palindromes and non-palindromes. Trace through the comparison loop to see exactly where and why the check fails.
Palindrome Checker: What is the result of the following code snippet when the input is "RaceCar"?
String word = input.toUpperCase();
String first = word.substring(0, 1);
String last = word.substring(word.length() - 1);
System.out.println(first + last);
RC
Rr
RA
RR
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods like toUpperCase() and substring(). String manipulation in Java involves extracting parts of strings using substring() with proper index understanding, where zero-based indexing is crucial. In this problem, the code converts 'RaceCar' to uppercase 'RACECAR', then extracts the first character using substring(0, 1) which gives 'R', and the last character using substring(word.length() - 1) which gives 'R'. The concatenation 'R' + 'R' results in 'RR'. Choice C is correct because both the first and last characters of 'RACECAR' are 'R'. Choice A shows mixed case which is impossible after toUpperCase(), B incorrectly identifies the last character, and D also misidentifies the last character. To help students: Remember that substring(start, end) excludes the character at the end index, and substring(start) goes to the end of the string. Practice identifying first and last characters using proper indexing. Watch for: off-by-one errors and forgetting that toUpperCase() affects the entire string.
Password Validator: What will be the output if the string is "Abc1-Def"?
String password = input;
password = password.replace("-", "");
boolean longEnough = password.length() >= 8;
boolean hasDigit = password.indexOf("1") >= 0;
System.out.println(longEnough && hasDigit);
false
true
True
FALSE
Explanation
This question tests AP Computer Science A string manipulation, specifically understanding and applying Java String methods with boolean logic. String manipulation in Java involves modifying strings and checking conditions, where understanding logical operators and string length is crucial. In this problem, 'Abc1-Def' becomes 'Abc1Def' after removing the hyphen, which has length 7. The code checks if length >= 8 (false) and if it contains '1' (true). The expression false && true evaluates to false. Choice A is correct because the password length (7) is less than 8, making the entire AND expression false regardless of the digit check. Choice B would require both conditions to be true, while C and D show incorrect capitalization for boolean values in Java. To help students: Remember that && requires BOTH conditions to be true for the result to be true. Practice evaluating compound boolean expressions step by step. Watch for: understanding that one false condition in an AND expression makes the entire result false.