Given two String variables s1 and s2 to determine if they ar

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

(s1.equals(s2))

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

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

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

length(s1) == length(s2)

(s1.equals(s2))

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

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

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

length(s1) == length(s2)

Solution

Option 2 should be used : (s1.length( ) == s2.length( ))

(s1.equals(s2)) : This would compare the values of strings s1 and s2
(s1.length( ).equals(s2)) : This would give Compile time error, as you cannot compare an int (s1.length()) to a String (s2)
(s1.length( ).equals(s2.length( )) : int(a primitive type) can\'t be compared using equals method. s1.length() and s2.length() return int type values.
length(s1) == length(s2) : There needs to be a length() method defined in your code to be able to use this way of comparing lengths.


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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site