KnowledgeBoat Logo

Computer Applications

The output of the statement "talent".compareTo("genius") is:

  1. 11
  2. –11
  3. 0
  4. 13

Java String Handling

ICSE 2024

4 Likes

Answer

13

Reason — The method compareTo(String str) in Java compares two strings lexicographically based on the ASCII values of characters. It returns:

0 if both strings are equal.
A positive value if the calling string comes after the argument string.
A negative value if the calling string comes before the argument string.

How It Works:
We compare "talent" and "genius" using their first differing characters:

  • The ASCII value of 't' = 116
  • The ASCII value of 'g' = 103

The difference is:
116 - 103 = 13

Since 't' comes after 'g', the result is 13.

Answered By

3 Likes


Related Questions