?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Methods with String Parameters that Call Each Other

APCSA-X4LFYN

Consider the following methods.

// Pre-condition: s has at least 2 characters
public static void insaneString(String s)
{
    int count = 0;
    for (int i=1; i<s.length(); i++)
    {
        if (s.substring(i,i+1).equals(s.substring(0,1)))
            count++;
    }
    if  (count>1)
        totallyAbsurdString(s.substring(s.length()-2) + s);
}

// Pre-condition: s has at least 2 characters
public static void totallyAbsurdString(String s)
{
    if (s.substring(0,1).equals(s.substring(s.length()-1)))
        insaneString(s.substring(s.length()-1) + s);
    else
        System.out.println(s);
}

What is output to the console when this segment of code is executed?

totallyAbsurdString("stars");
A
sstars
B
rssstars
C
ssstarss
D
rssstarss
E

a StackOverflowError is thrown because of an infinite loop.