?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Absurd String Method

APCSA-FZKGE4

Consider the following methods.

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

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

What is output to the console when the following code segment is executed?

absurdString("alpha");
A

aaalphaaa

B

alpha

C

a StackOverflowError is thrown (infinite loop)

D

aalpha

E

aalphaa