Computer Applications
Which of the following returns a value greater than or equal to 0?
Java String Handling
1 Like
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.
Answered By
2 Likes
Related Questions
The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)
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 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.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.