KnowledgeBoat Logo
|

Computer Applications

The output of the java statement "SOLDIER".compareTo("SOLUTE"); is

  1. -4
  2. -17
  3. 17
  4. 0

Java String Handling

1 Like

Answer

-17

Reason — The compareTo() method in Java compares two strings lexicographically using the Unicode values.

Let's compare "SOLDIER" and "SOLUTE":

  • The first three letters S, O, L are the same in both strings.
  • The 4th letter in "SOLDIER" is 'D' and in "SOLUTE" it is 'U'.
  • Unicode of 'D' = 68, Unicode of 'U' = 85.
  • 68 − 85 = -17

So, the output is -17.

Answered By

3 Likes


Related Questions