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 a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
Two strings,
city1
andcity2
, are compared usingcity1.compareTo(city2)
, and the result is less than zero. What does this indicate?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.
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); }