After the following code fragment, what is the value in fname?

String str;
int fname;
str = "Foolish boy.";
fname = str.indexOf("fool");

Options
- 0
- 2
- -1
- 4


CORRECT ANSWER : -1

Discussion Board
query

please explain in detail that how -1 is correct answer?

manisha 11-7-2016 06:49 AM

indexOf

pls explain it with examples

shweta Nitin patil 03-30-2015 09:09 AM

indexOf

pls explain it with examples

shweta Nitin patil 03-30-2015 09:09 AM

java

-1 ans is right, because indexOf() method return -1 if character passed in the method is not present in the string .
Our string is "Foolish boy" and we are searching for "fool" (small f not F).

prashant 08-8-2014 06:39 AM

indexOf()

indexOf() has many different variants and it is represented as:

public int indexOf(int ch): Returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.

public int indexOf(int ch, int fromIndex): Returns the index within this string of the first occurrence of the specified character, starting the search at the specified index or -1 if the character does not occur.

int indexOf(String str): Returns the index within this string of the first occurrence of the specified substring. If it does not occur as a substring, -1 is returned.

int indexOf(String str, int fromIndex): Returns the index within this string of the first occurrence of the specified substring, starting at the specified index. If it does not occur, -1 is returned.

So, from all these methods it is clear that the search query wasn't able to find the specified character that is "fool". So, it will return the result as -1.


Rohit Sharma 07-29-2014 02:00 AM

answer to this que

Ans will be -1, because indexOf() method return -1 if character passed in the method is not present in the string .
Our string is "Foolish boy" and we are searching for "fool" (small f not F).
Thats why our answer will be -1

unknown 07-24-2014 10:45 AM

Re : Query

Yes, U R right Raghu.
It will take if it is der,but in dis case string which we are passing is not der...
Got it :)

Shiva 07-21-2014 10:17 PM

Query

How it comes -1, it takes the index of what ever the string we passed as an argument

Raghu Varma Pinnelli 04-14-2014 01:30 AM

Re: query

Ram kumar bhaiye, isko run karke dekho -1 answer ayega, aur input string me 'F' hai na kee 'f' isliye

Bhaisaab 11-30-2013 08:53 AM

query

kaise -1 hoga bhaiya
detail me btaiye

Ram kumar 11-18-2013 12:12 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement