QUESTION IMAGE
Question
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)
f) none of these
- using string class methods (substring)
given the following declaration: string s = \apples and bananas are yummy.\
Step1: Analyze option a
s1.equals(s2) checks if the string contents are identical, not lengths.
Step2: Analyze option b
s1.length() returns an int, which has no equals() method for a String s2; this is invalid.
Step3: Analyze option c
s1.length() returns an int, which cannot call equals() on another int return value; this is invalid.
Step4: Analyze option d
s1.length() and s2.length() return int values. The == operator correctly compares integer values to check if lengths are equal.
Step5: Analyze option e
length(s1) is not valid syntax for Java Strings; the correct method call is s1.length().
Step6: Analyze option f
Since option d is valid, this is incorrect.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
d) (s1.length( ) == s2.length( ))