Problem of the Day
Problem:
A boolean method check
is shown here:
public static boolean check(String word)
{
String vowels = "aeiou";
return (vowels.indexOf(word.substring(0,1)) >= 0 &&
vowels.indexOf(word.substring(word.length() - 1)) < 0) ||
(vowels.indexOf(word.substring(0,1)) < 0 &&
vowels.indexOf(word.substring(word.length() - 1)) >= 0);
}
Under what conditions does the boolean method return the value true
?
- When the first and last letters of the string are both vowels
- When the first letter is a vowel and the last letter is a consonant
- When the first letter is a consonant and the last letter is a vowel
- I only
- II only
- III only
- I or II only
- II or III only
The correct answer is e. The check
method is looking for first and last letters that are either consonant-vowel or vowel-consonant, using the value of the indexOf
. The method indexOf
returns a positive value if a letter is found in the String vowels
(and therefore a vowel), or a -1
result if the letter is not found in vowels
(and therefore a consonant).