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. 8


Correct Option: A
Explanation:

To solve this question, the user needs to know what the indexOf() method does in Java. The indexOf() method returns the index of the first occurrence of a specified substring within a string, or -1 if the substring is not found.

In the given code fragment, the variable s is assigned the string "Foolish boy.", and the indexOf() method is called on this string with the argument "fool". Since the substring "fool" is not present in the original string (due to the capital letter 'F' in the string), the indexOf() method returns -1.

Therefore, the correct answer is:

The Answer is: D. -1

Find more quizzes: