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

  1. -1

  2. 0

  3. 4

  4. random value


Correct Option: A
Explanation:

To solve this question, the user needs to understand the behavior of the indexOf() method in Java. This method returns the index of the first occurrence of a specified substring within a given string. If the substring is not found in the string, the method returns -1.

In the given code fragment, the string s is initialized to "Foolish boy." and the indexOf() method is called on this string with the argument "fool". Note that the substring "fool" is not present in the original string with the same letter case. The index of the first occurrence of the substring "fool" in the string s is thus -1.

Therefore, the correct answer is:

The Answer is: A. -1

Find more quizzes: