Learn AP Comp Sci

Problem of the Day

Monday, February 16, 2026


Problem:

A String array of the five vowels has been initialized.

public static String[] vowels = {"a","e","i","o","u"};

The method isVowel, shown below, works as designed.

/**
* Returns true if letter is one of the five
* vowels "a", "e", "i", "o", or "u"
*/
public static boolean isVowel(String letter)
{
/* code not shown */
}

Consider the following incomplete method, which is intended to identify words that have at least one of each of the five vowels in them ("a", "e", "i", "o", and "u").

public static boolean hasAllFiveVowels(String word)
{

/* missing code */

}

Which of the following should replace /* missing code */ so that the method works as intended?


  1. for (int i = 0; i < word.length(); i++)
    {
    if (!isVowel(word.substring(i, i+1)))
    return false;
    }
    return true;

  2. int vowelCount = 0;
    for (int i = 0; i < word.length(); i++)
    {
    if (isVowel(word.substring(i, i+1)))
    vowelCount = vowelCount + 1;
    }
    return vowelCount == 5;

  3. ArrayList vowelList = new ArrayList();
    for (String vowel : vowels)
    vowelList.add(vowel);
    for (int i = 0; i < vowelList.size(); i++)
    {
    if (word.indexOf(vowelList.get(i)) == -1)
    return false;
    else
    vowelList.remove(i);
    }
    return true;

  4. ArrayList vowelList = new ArrayList();
    for (String vowel : vowels)
    vowelList.add(vowel);
    while (vowelList.size() > 0)
    {
    if (word.indexOf(vowelList.get(vowelList.size() - 1)) == -1)
    return false;
    else
    vowelList.remove(vowelList.size() - 1);
    }
    return true;

Show solution:

The correct answer is d. This code's strategy is to work backwards through the vowelList ArrayList, removing vowels from that list if they are found in the word. If at any point we get to a vowel that isn't found (indicated by indexOf returning a value of -1.

Answer c attempts to do the same thing by moving forward through the list, but because the index variable increments in the for-loop, it ends up skipping over every other vowel during its search, and sometimes returns a wrong result.