Computer Applications
What will the following code output:
String s = "malayalam";
System.out.println(s.indexOf('m'));
System.out.println(s.lastIndexOf('m'));
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.
Related Questions
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024