Computer Applications
Two strings, city1
and city2
, are compared using city1.compareTo(city2)
, and the result is less than zero. What does this indicate?
Java String Handling
3 Likes
Answer
city1
appears before city2
in an alphabetical list.
Reason — The method compareTo(String s)
in Java compares two strings lexicographically. It returns:
- A negative value if the calling string (
city1
) comes before the argument string (city2
) in alphabetical order. - 0 if the two strings are equal.
- A positive value if the calling string (
city1
) comes after the argument string (city2
) in alphabetical order.
If the result is less than zero, it indicates that city1
appears before city2
lexicographically (alphabetically).
Answered By
2 Likes
Related Questions
Define a class to accept the gmail id and check for its validity.
A gmail id is valid only if it has:
→ @
→ .(dot)
→ gmail
→ com
Example:
icse2024@gmail.com
is a valid gmail idThe output of the statement "talent".compareTo("genius") is:
- 11
- –11
- 0
- 13
Consider the following program segment in which the statements are jumbled, choose the correct order of statements to check if a given word is Palindrome or not.
boolean palin(String w) { boolean isPalin; w = w.toUpperCase(); int l = w.length(); isPalin = false; // Stmt (1) for (int i = 0; i < l / 2; i++) { char c1 = w.charAt(i), c2 = w.charAt(l - 1 - i); // Stmt (2) if (c1 != c2) { break; // Stmt (3) isPalin = true; // Stmt (4) } } return isPalin; }
Which of the following returns a value greater than or equal to 0?