?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Find Highest Character in String

APCSA-E6GPLV

Which of the following methods will return the maximum character in the String (based on the java sorting sequence)?

I.

// Pre-condition: s is at least one character long
public static String findHighestChar(String s)
{
    String max = s.substring(s.length()-1);

    for (int i=1; i<s.length(); i++)
    {
        if (s.substring(i-1,i) > max)
            max = s.substring(i-1,i);
    }
    return max;
}

II.

// Pre-condition: s is at least one character long
public static String findHighestChar(String s)
{
    String max = " ";

    for (int i=0; i<s.length(); i++)
    {
        if(s.substring(i,i+1).compareTo(max)>0
            max = s.substring(i,i+1);
    }
    return max;
}

III.

// Pre-condition: s is at least one character long
public static String findHighestChar(String s)
{
    String max = s.substring(s.length()-1);
    for (int i=1; i<s.length(); i++)
    {
        if (s.substring(i-1,i).compareTo(max)<0)
            continue;
        else
           max = s.substring(i-1,i);
    }
    return max;
 }
A

I only.

B

II only.

C

III only.

D

I and III only.

E

II and III only.