Learn AP Comp Sci

Problem of the Day

Friday, January 9, 2026


Problem:

An ArrayList of String values arr contains at least one value. What does the following code print?

int x = 0;
for (int i = 1; i < arr.size(); i++)
{
if (arr.get(i).length() < arr.get(x).length())
x = i;
}
System.out.println(x);
  1. the string in the series that is first alphabetically
  2. the index of the string in the series that is first alphabetically
  3. the string in the series that is shortest
  4. the index of the string in the series that is shortest

Show solution:

The correct answer is d. The variable x begins with a value of 0, representing the first position in the ArrayList. The loop runs through and compares the length of the other strings in the ArrayList, and resets x to the index of any values that have a smaller length.