Computer Applications
What will the following code output:
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
Java
Java String Handling
ICSE 2012
13 Likes
Answer
0
8
Working
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.
Answered By
5 Likes
Related Questions
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)