Problem of the Day
Wednesday, December 10, 2025
Problem:
An Array of String values arr contains at least one value. What does the following code print?
int r = 0;
for (int i = 1; i < arr.length; i++)
{
if (arr[i].compareTo(arr[r]) < 0)
r = i;
}
System.out.println(arr[r]);
- the string in the series that is first alphabetically
- the index of the string in the series that is first alphabetically
- the string in the series that is shortest
- the index of the string in the series that is shortest
The correct answer is a. The variable r is initialized to the first index 0, and then a loop runs through the rest of the values, comparing each one with the value of the current arr[r]. The compareTo method here compares two String values and returns a negative results if the first String comes before the second. Thus, we're looking for—and saving the index of—strings that come earlier alphabetically. Once the loop has completed we print the String to which arr[r] refers, the first string (alphabetically) in the series.