?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Difficult

Printing String Values within a Loop

APCSA-ESBWVV

Consider the following code segment.

String str1 = "rat";
String str2 = "";

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

What is printed as a result of executing the code segment?

A
ratequal
B
rraratequal
C
tarnot equal
D
equal
E
ttatarnot equal