KnowledgeBoat Logo

Computer Applications

Which of the following returns a value greater than or equal to 0?

Java String Handling

3 Likes

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 than 0.
  • 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 or false), not a numeric value.

Answered By

3 Likes


Related Questions