?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Looping Through String and Comparing Characters

APCSA-EKEWIA

Consider the following method.

public static String compareSubs(String s)
{
    for (int i=s.length()-1; i>0; i--)
    {
        if (s.substring(i,i+1).compareTo(s.substring(i-1,i))>0)
            return s.substring(i,i+1) + " " + s.substring(i-1,i);
    }
    return s;
}

Assuming the parameter s is at least two characters long, when will the compareSubs method return the exact value of the parameter s?

A

When the characters in parameter s are all in descending sequence from the first position to last position.

B

When the characters in parameter s are all in ascending sequence from the first position to the last position.

C

When it first encounters a character that is less than the previous character.

D

It will always return the parameter value.

E

It will never return the parameter value.