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
11 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
4 Likes
Related Questions
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.
Write the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));
The output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13