?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

Testing the Equality of Two Strings

APCSA-GT1FVN

Consider the following code segment.

String str1 = "elephant";
String str2 = "";   // no spaces between the double quotes

for (int i=str1.length()-1; i>=0; i--)
{
    str2 = str1.substring(i,i+1) + str2;
    if (str1.equals(str2) || i<=0)
        System.out.print(str2 + " ");
}
if (str1.equals(str2))
    System.out.println("equal");
else
    System.out.println("not equal");

What does this segment of code print to the console?

A

tnahpele not equal

B

equal

C

elephant equal

D

not equal

E

t nt ant hant phant ephant lephant elephant equal