Asked by Jeanette Abreu on May 12, 2024

verifed

Verified

If str1 is "Hello" and str2 is "Hi", which of the following could not be a result of str1.compareTo(str2) ;?

A) -9
B) -5
C) -1
D) 1

CompareTo Method

A method in Java that is used for comparing two objects or values, typically returning a negative integer, zero, or a positive integer based on the comparison.

  • Identify and utilize methods for string comparison in Java.
verifed

Verified Answer

JS
Julianne Sol CruzMay 17, 2024
Final Answer :
D
Explanation :
When comparing "Hello" to "Hi", the result of str1.compareTo(str2) will always be negative since "H" (the first letter of "Hello") comes after "H" (the first letter of "Hi") in the alphabetical order. Therefore, choices A, B, and C are all possible results. However, choice D, which is a positive number, could not be a result of str1.compareTo(str2).