Problem of the Day
Monday, January 5, 2026
Problem:
The static method reverse is intended to take a string of text and return a new string with the same letters in reverse order. The outline of the method is given here.
public static String reverse(String text)
{
String r = "";
/* missing code */
return r;
}
Which of the following staements replacing /* missing code */ would not successfully reverse the characters in the string?
for (int i = text.length() - 1; i >= 0; i--)
r += text.substring(i, i + 1);for (int i = 0; i < text.length(); i++)
r = text.substring(i, i + 1) + r;for (int i = text.length(); i > 0; i--)
r += text.substring(i, i + 1);- All three code segments will successfully reverse the code
The correct answer is c—that code will not reverse the letters in the string. The index variable for the loop is meant to go from the last position to the first. Because indexing of letters begins at position 0, the index of the last character is text.length() - 1. Loop (c), as written, will result in an IndexOutOfBounds exception.