Skip to main content

Unit 1 · Using Objects and Methods

String Immutability

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What string immutability covers

Strings in Java cannot be modified in place. Methods such as substring, toUpperCase, and replace return a brand-new String and leave the original untouched. A variable only changes when you assign the result back to it. Comparison follows the same object logic: equals compares character content, while == compares references and can be false for two Strings with identical text.

Where students lose points

Calling s.toUpperCase(); without assigning the result and then expecting s to have changed is the defining error of this topic. Students also use == to compare String content, which may appear to work for literals because of interning and then fail for Strings built at runtime.

How it shows up on the AP exam

Unit 1 content with consequences across the whole exam. Immutability explains why String parameters cannot be changed by a method, which is a recurring MCQ theme and a frequent source of lost FRQ points.

For practice use only.

String Immutability MCQ Practice

AP CSA String immutability practice: why String methods return new values, when concatenation actually changes a variable, and how == differs from equals.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Which option matches the complete output produced by this segment?

public class StringImmutabilityContext
{
    public static void runExample()
    {
        String s = "cat";
        s.substring(0, 2);
        System.out.print(s);
    }

    public static void main(String[] args)
    {
        runExample();
    }
}
Choose one answer.