🎴 Flashcard Mode

Java Practice Test - 3

Card1 / 25
Mastered0
Review0
QuestionClick to flip

After the following code fragment, what is the value in fname?
String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");

AnswerClick to flip back
A
-1
💡 Explanation:

The indexOf() method in Java is case-sensitive. The string str is 'Foolish boy.' with capital 'F' in 'Fool'. When we search for the lowercase substring 'fool', it doesn't match because Java considers character case. Since the substring is not found, indexOf() returns -1, which is the standard behavior indicating 'substring not present.'

Change Mode