ICSE Class 10 Computer Applications Question 6 of 36

String Handling — Question 7

Back to all questions
7
Question

Question 7

What will the following code output ?

String s = "malayalam" ;
System.out.println(s.indexOf('m')); 
System.out.println(s.lastIndexOf('m'));
Answer
Output
0
8
Explanation

indexOf() returns the index of the first occurrence of the specified character within the current String object. The first occurrence of 'm' is at index 0, thus 0 is printed first.

lastIndexOf() returns the index of the last occurrence of the specified character within the String object. The last occurrence of 'm' is at index 8, thus 8 is printed next.