AP CSA Unit-Level MCQ

String Indexing

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

What string indexing covers

String positions are zero-based, so the first character sits at index 0 and the last sits at length() - 1. The charAt method returns the character at a position, and length() is a method with parentheses, unlike the length field on an array. Any index that is negative or greater than or equal to length() throws a StringIndexOutOfBoundsException.

Where students lose points

Using length() as a valid index is the standard mistake, and it produces a runtime exception rather than a wrong value. Students also write length without parentheses for Strings or add parentheses to array length, and they confuse the character at index 1 with the first character.

How it shows up on the AP exam

Unit 1 String methods. Indexing accuracy is a prerequisite for every substring and traversal question, so an error here cascades into the rest of a trace.

For practice use only.

String Indexing MCQ Practice

AP CSA String indexing practice: zero-based positions, charAt, length versus last index, and the out-of-bounds errors the exam tests most.

Question 1 of 15

Answered 0 of 15

Choose one answer.

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

String word = "ALGORITHM";
int start = 0;
String piece = word.substring(start, start + 3);
System.out.print(piece);

All 15 string indexing questions

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

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

    String word = "ALGORITHM";
    int start = 0;
    String piece = word.substring(start, start + 3);
    System.out.print(piece);
    • ALG
    • LG
    • AL
    • A runtime exception is thrown.
    Show worked solution

    Correct answer: ALG

    substring includes the starting index and excludes the ending index.

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

    String word = "PROGRAM";
    String target = "OG";
    int index = word.indexOf(target);
    System.out.print(index + ":" + word.substring(index, index + 2));
    • -1:
    • 2:OG
    • 3:OG
    • 2:O
    Show worked solution

    Correct answer: 2:OG

    indexOf returns the first starting position of target. substring then uses that index to extract two characters.

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

    String word = "METHODS";
    word.substring(1);
    word.toLowerCase();
    System.out.print(word);
    • ETHODSM
    • METHODS
    • ETHODS
    • METHOD
    Show worked solution

    Correct answer: METHODS

    String methods return new String values. Since the results are not assigned, word is unchanged.

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

    String word = "ARRAYS";
    String out = "";
    for (int i = 0; i < word.length(); i += 2) {
      out += word.substring(i, i + 1);
    }
    System.out.print(out);
    • AR
    • YRA
    • ARY
    • ARRAYS
    Show worked solution

    Correct answer: ARY

    The loop appends the character at every even index.

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

    String word = "CODING";
    String marker = "I";
    int count = 0;
    for (int i = 0; i < word.length(); i++) {
      if (word.substring(i, i + 1).compareTo(marker) >= 0) {
        count++;
      }
    }
    System.out.print(count);
    • 4
    • 2
    • 6
    • 3
    Show worked solution

    Correct answer: 3

    Each one-character substring is compared lexicographically with marker.

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

    String s = "computer";
    System.out.print(s.charAt(0) + "" + s.charAt(s.length() / 2)
            + s.charAt(s.length() - 1));
    • cot
    • cpr
    • cur
    • cmr
    Show worked solution

    Correct answer: cur

    The indexes selected are 0, 4, and 7, which hold c, u, and r.

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

    String s = "abcdef";
    String out = "";
    for (int i = s.length() - 1; i >= 0; i -= 2)
    {
        out += s.charAt(i);
    }
    System.out.print(out);
    • eca
    • fedcba
    • ace
    • fdb
    Show worked solution

    Correct answer: fdb

    Counting down by two from index 5 visits 5, 3, and 1.

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

    String s = "level";
    int c = 0;
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == s.charAt(s.length() - 1 - i)) { c++; }
    }
    System.out.print(c);
    • 5
    • 2
    • 3
    • 0
    Show worked solution

    Correct answer: 5

    Every mirrored pair matches in a palindrome, and the loop checks all five positions rather than half.

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

    String s = "banana";
    int last = -1;
    for (int i = 0; i < s.length(); i++)
    {
        if (s.charAt(i) == 'a') { last = i; }
    }
    System.out.print(last);
    • 1
    • 5
    • 3
    • -1
    Show worked solution

    Correct answer: 5

    The loop never stops early, so the variable keeps the index of the final match.

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

    String s = "abcabc";
    System.out.print(s.indexOf('b') + s.lastIndexOf('b'));
    • 1
    • 4
    • 5
    • 6
    Show worked solution

    Correct answer: 5

    The first b sits at index 1 and the last at index 4, and the two int values are added.

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

    String s = "xyz";
    String out = "";
    for (int i = 0; i < s.length(); i++)
    {
        for (int j = 0; j <= i; j++)
        {
            out += s.charAt(j);
        }
    }
    System.out.print(out);
    • xyzxyz
    • xyz
    • xxxyyz
    • xxyxyz
    Show worked solution

    Correct answer: xxyxyz

    Each outer pass appends a growing prefix of the string.

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

    String s = "abcde";
    int i = 0;
    int j = s.length() - 1;
    String out = "";
    while (i < j)
    {
        out += s.charAt(i) + "" + s.charAt(j);
        i++;
        j--;
    }
    System.out.print(out);
    • aebd
    • abcde
    • aebdc
    • eadb
    Show worked solution

    Correct answer: aebd

    Two indexes move toward each other and stop before meeting, so the middle character is never appended.

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

    String s = "hello";
    char c = s.charAt(1);
    System.out.print(c + 1);
    • e1
    • 102
    • f
    • 101
    Show worked solution

    Correct answer: 102

    A char promotes to its numeric value in arithmetic, and the letter e has the value 101.

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

    String s = "aabbcc";
    String out = "";
    for (int i = 0; i < s.length(); i += 2)
    {
        if (s.charAt(i) == s.charAt(i + 1))
        {
            out += s.charAt(i);
        }
    }
    System.out.print(out);
    • aabbcc
    • aa
    • abc
    • ab
    Show worked solution

    Correct answer: abc

    Each pair is compared once, and all three pairs match.

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

    String s = "abc";
    System.out.print(s.charAt(0) == 'a');
    System.out.print(" " + (s.substring(0, 1) == "a"));
    • true true
    • false false
    • false true
    • true false
    Show worked solution

    Correct answer: true false

    Comparing a char with == is correct, but comparing a substring built at run time with == compares references.