Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?


Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?


a) (s1.equals(s2))

b) (s1.length( ).equals(s2))

c) (s1.length( ).equals(s2.length( ))

d) (s1.length( ) == s2.length( ))

e) length(s1) == length(s2)

Answer: d. Explanation: Since s1 and s2 are Strings, we can only obtain their length by passing the length( ) message to each one. The length( ) method returns an int that we can directly compare against another int using ==, as done in d. The answers for b and c are syntactically invalid since s1.length( ) returns an int, and so cannot be passed the message .equals( ). The answer in e is also syntactically invalid. Finally, the answer in a determines if the two Strings are the exact same. While their lengths will be equal if this is true, two Strings may have equal length but not be equal, so this is logically incorrect.