AP CSA Unit-Level MCQ

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.

Code Tracing 01 - String Immutability: track the final printed value.

String base = "ALGORITHM";
String copy = base;
copy += "X";
System.out.print(base + ":" + copy);

All 15 string immutability questions

Work through the interactive quiz above first. This is the full string immutability question set with worked solutions, so students can review any question after attempting it.

  1. 1.Code Tracing 01 - String Immutability: track the final printed value.

    String base = "ALGORITHM";
    String copy = base;
    copy += "X";
    System.out.print(base + ":" + copy);
    • ALGORITHM:ALGORITHMX
    • ALGORITHMX:ALGORITHMX
    • ALGORITHM:ALGORITHM
    • ALGORITHMX:ALGORITHM
    Show worked solution

    Correct answer: ALGORITHM:ALGORITHMX

    Appending builds a brand-new String and points copy at it. The original base still refers to the unchanged value.

  2. 2.Code Tracing 12 - String Immutability: follow the variable updates.

    String a = "PROGRAM";
    String b = new String("PROGRAM");
    System.out.print((a == b) + ":" + a.equals(b));
    • true:false
    • false:true
    • true:true
    • false:false
    Show worked solution

    Correct answer: false:true

    == compares whether two references point at the same object, while equals compares the characters. new String() forces a separate object.

  3. 3.Code Tracing 28 - String Immutability: trace the branch and loop path.

    String value = "METHODS";
    value.toUpperCase();
    value = value.concat("!");
    System.out.print(value);
    • A runtime exception is thrown.
    • METHODS!
    • METHODS
    • The code does not compile.
    Show worked solution

    Correct answer: METHODS!

    The toUpperCase result is discarded because it is never assigned. Only the concat line changes what value refers to.

  4. 4.Code Tracing 39 - String Immutability: watch the index changes.

    String word = "ARRAYS";
    String out = word.replace(word.charAt(0), 'Z');
    System.out.print(word + ":" + out);
    • ARRAYS:ARRAYS
    • ZRRZYS:ARRAYS
    • ARRAYS:ZRRZYS
    • ZRRZYS:ZRRZYS
    Show worked solution

    Correct answer: ARRAYS:ZRRZYS

    replace returns a new String and swaps every matching character, leaving the original untouched.

  5. 5.Code Tracing 50 - String Immutability: evaluate the state change step by step.

    String a = "CODING";
    String b = a;
    a += "Q";
    System.out.print(a.equals(b) + ":" + b);
    • true:CODINGQ
    • false:CODINGQ
    • true:CODING
    • false:CODING
    Show worked solution

    Correct answer: false:CODING

    Reassigning a does not follow through to b, because b still refers to the original immutable String.

  6. 6.What is printed by the following code segment?

    String a = "cat";
    String b = a;
    a += "s";
    System.out.print(a + " " + b);
    • cats cats
    • cat cat
    • cats cat
    • cat cats
    Show worked solution

    Correct answer: cats cat

    Concatenation builds a new string and rebinds a, leaving b pointing at the original.

  7. 7.What is printed by the following code segment?

    String s = "abc";
    s.toUpperCase();
    s.concat("d");
    s = s.replace('a', 'z');
    System.out.print(s);
    • ABCD
    • abc
    • ZBCD
    • zbc
    Show worked solution

    Correct answer: zbc

    Only the call whose result is assigned has any effect, and the first two are discarded.

  8. 8.What is printed by the following code segment?

    String a = "x";
    String c = new String("x");
    System.out.print((a == c) + " " + a.equals(c));
    • false true
    • true true
    • false false
    • true false
    Show worked solution

    Correct answer: false true

    Using new always builds a separate object, so the reference comparison fails while equals succeeds.

  9. 9.What is printed by the following code segment?

    String s = "abc";
    String t = s.substring(0);
    System.out.print((s == t) + " " + s.equals(t));
    • false true
    • true true
    • true false
    • false false
    Show worked solution

    Correct answer: true true

    Asking for the whole string back returns the same object, so even the reference comparison is true here.

  10. 10.What is printed by the following code segment?

    String s = "a";
    for (int i = 0; i < 3; i++)
    {
        s.concat("b");
    }
    System.out.print(s);
    • abbb
    • ab
    • a
    • aaa
    Show worked solution

    Correct answer: a

    Every result is discarded, so the loop leaves the original string untouched.

  11. 11.What is printed by the following code segment?

    String s = "hello";
    String t = s.replace('l', 'L');
    System.out.print(s + " " + t);
    • heLLo heLLo
    • hello hello
    • heLLo hello
    • hello heLLo
    Show worked solution

    Correct answer: hello heLLo

    The original is never modified, and the replacement is returned as a new string.

  12. 12.What is printed by the following code segment?

    String a = "12";
    int n = 2;
    String b = "1" + n;
    System.out.print(a.equals(b) + " " + (a == b));
    • true false
    • true true
    • false false
    • false true
    Show worked solution

    Correct answer: true false

    The characters match, but the concatenation involving an int is performed at run time and produces a separate object.

  13. 13.What is printed by the following code segment?

    String s = "abc";
    char[] c = s.toCharArray();
    c[0] = 'z';
    System.out.print(s + " " + new String(c));
    • zbc zbc
    • abc zbc
    • abc abc
    • zbc abc
    Show worked solution

    Correct answer: abc zbc

    The char array is a copy, so changing it cannot alter the immutable string it came from.

  14. 14.What is printed by the following code segment?

    String s = "aaa";
    String t = s.replace("a", "aa");
    System.out.print(s.length() + " " + t.length());
    • 6 6
    • 3 3
    • 3 6
    • 6 3
    Show worked solution

    Correct answer: 3 6

    Each character is replaced by two, doubling the length of the returned string while the original is unchanged.

  15. 15.What is printed by the following code segment?

    String s = "";
    for (int i = 0; i < 4; i++)
    {
        s += i;
    }
    System.out.print(s.length() + s);
    • 0123
    • 4
    • 01234
    • 40123
    Show worked solution

    Correct answer: 40123

    The loop builds a four-character string, and its length is concatenated in front of it.