Computer Applications
Answer
length()
Reason — Analyzing the given options:
1. lastIndexOf(char ch)
:
- Returns the last index of the specified character in the string.
- If the character is not found, it returns
-1
, which can be less than0
. - Not always greater than or equal to 0.
2. length()
:
- Returns the number of characters in the string.
- This is always greater than or equal to 0, as an empty string has a length of
0
.
3. compareTo(String s)
:
- Compares two strings lexicographically.
- Can return negative, 0, or positive values, depending on the comparison result.
- Not always greater than or equal to 0.
4. equalsIgnoreCase(String s)
:
- Returns a boolean value (
true
orfalse
), not a numeric value.
Related Questions
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));
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@2024Two strings,
city1
andcity2
, are compared usingcity1.compareTo(city2)
, and the result is less than zero. What does this indicate?The output of a program which extracts a part of the string "SUBMISSION" is as follows:
(a) "MISS"
(b) "MISSION"If
String str = "SUBMISSION";
write appropriate Java statements to get the above outputs.