AP CSA Unit-Level MCQ

substring and indexOf

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

What substring and indexof covers

The call substring(a, b) returns characters from index a up to but not including index b, so the result has b - a characters. The one-argument form substring(a) runs from index a to the end of the String. The indexOf method returns the index of the first occurrence of a target, or -1 when the target does not appear at all.

Where students lose points

Treating the end index as inclusive is the dominant error and produces an answer one character too long. The other frequent failure is passing an unchecked indexOf result straight into substring: when indexOf returns -1 the substring call throws. Students also forget that substring(a, a) legally returns an empty String.

How it shows up on the AP exam

Unit 1 String methods, and a heavily tested combination because a single line can test index arithmetic, method semantics, and boundary handling at once. Word- and token-parsing FRQs lean on it directly.

For practice use only.

substring and indexOf MCQ Practice

AP CSA substring and indexOf practice: inclusive start and exclusive end, computing result length, and handling the -1 that indexOf returns when nothing matches.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Code Tracing 01 - substring and indexOf: track the final printed value.

String word = "ALGORITHM";
int idx = word.indexOf("QZ");
System.out.print(idx);

All 15 substring and indexof questions

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

  1. 1.Code Tracing 01 - substring and indexOf: track the final printed value.

    String word = "ALGORITHM";
    int idx = word.indexOf("QZ");
    System.out.print(idx);
    • 9
    • 1
    • -1
    • 0
    Show worked solution

    Correct answer: -1

    indexOf returns -1 when the target does not occur at all. It does not throw an exception.

  2. 2.Code Tracing 12 - substring and indexOf: follow the variable updates.

    String word = "PROGRAM";
    String text = word + word;
    int first = text.indexOf("OG");
    int second = text.indexOf("OG", first + 1);
    System.out.print(first + ":" + second);
    • 2:2
    • 2:-1
    • 9:2
    • 2:9
    Show worked solution

    Correct answer: 2:9

    The two-argument indexOf starts searching at the given index, which is how you find a later occurrence.

  3. 3.Code Tracing 28 - substring and indexOf: trace the branch and loop path.

    String word = "METHODS";
    String head = word.substring(0, 3);
    String tail = word.substring(3);
    System.out.print(head + "|" + tail);
    • HODS|MET
    • METH|ODS
    • MET|METHODS
    • MET|HODS
    Show worked solution

    Correct answer: MET|HODS

    One-argument substring runs to the end of the string, while the two-argument form stops before its ending index.

  4. 4.Code Tracing 39 - substring and indexOf: watch the index changes.

    String word = "ARRAYS";
    String tail = word.substring(word.length());
    System.out.print("[" + tail + "]" + tail.length());
    • []0
    • A runtime exception is thrown.
    • [ARRAYS]6
    • []1
    Show worked solution

    Correct answer: []0

    Calling substring at exactly length() is legal and returns an empty string. Only an index past length() throws.

  5. 5.Code Tracing 50 - substring and indexOf: evaluate the state change step by step.

    String word = "CODING";
    System.out.print(word.substring(2, word.length() + 1));
    • (no output)
    • A runtime exception is thrown.
    • DING
    • CODING
    Show worked solution

    Correct answer: A runtime exception is thrown.

    The ending index may equal length() but not exceed it, so this throws StringIndexOutOfBoundsException.

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

    String s = "one,two,three";
    int a = s.indexOf(",");
    int b = s.indexOf(",", a + 1);
    String mid = s.substring(a + 1, b);
    System.out.print(mid);
    • ,two
    • two,
    • one
    • two
    Show worked solution

    Correct answer: two

    The two separator positions bracket the middle field, and the exclusive end index leaves out the second comma.

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

    String s = "abcdefgh";
    String head = s.substring(2, 5);
    System.out.print(head + s.substring(5).length());
    • cde3
    • cde5
    • cdefgh3
    • cde8
    Show worked solution

    Correct answer: cde3

    The first call returns three characters and the second returns the remaining three, whose length is appended.

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

    String s = "aXbXc";
    String out = "";
    int i = s.indexOf("X");
    while (i >= 0)
    {
        out += i;
        i = s.indexOf("X", i + 1);
    }
    System.out.print(out);
    • 12
    • 13
    • 135
    • 1
    Show worked solution

    Correct answer: 13

    The separator appears at indexes 1 and 3, and each is appended as text.

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

    String s = "programming";
    int a = s.indexOf("gram");
    int b = s.indexOf("ming");
    System.out.print(s.substring(a, b));
    • gramming
    • ming
    • gram
    • program
    Show worked solution

    Correct answer: gram

    The start index is 3 and the end index is 7, so exactly the four characters of the first word are returned.

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

    String s = "abc";
    String tail = s.substring(3);
    System.out.print(tail + "|" + tail.length());
    • abc|3
    • |3
    • c|1
    • |0
    Show worked solution

    Correct answer: |0

    A start index equal to the length is legal and returns an empty string rather than throwing.

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

    String s = "hello world";
    int sp = s.indexOf(" ");
    String a = s.substring(sp + 1);
    String b = s.substring(0, sp);
    System.out.print(a + " " + b);
    • world hello
    • hello world
    • world hello
    • worldhello
    Show worked solution

    Correct answer: world hello

    The two halves are swapped around the separator, and the explicit space rejoins them.

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

    String s = "aaa";
    int c = 0;
    int i = s.indexOf("aa");
    while (i >= 0)
    {
        c++;
        i = s.indexOf("aa", i + 1);
    }
    System.out.print(c);
    • 1
    • 2
    • 3
    • 0
    Show worked solution

    Correct answer: 2

    Resuming one past the previous match counts overlapping occurrences, and there are two.

  13. 13.What is the result of executing the following code segment?

    String s = "data";
    int i = s.indexOf("z");
    System.out.print(s.substring(i));
    • An empty string is printed.
    • data is printed.
    • A StringIndexOutOfBoundsException is thrown.
    • The code does not compile.
    Show worked solution

    Correct answer: A StringIndexOutOfBoundsException is thrown.

    The search fails and returns -1, and passing a negative start index to substring throws at run time.

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

    String s = "abcabcabc";
    int a = s.lastIndexOf("abc");
    int b = s.indexOf("abc", 4);
    System.out.print(a + " " + b);
    • 6 3
    • 0 6
    • 3 6
    • 6 6
    Show worked solution

    Correct answer: 6 6

    The last occurrence begins at index 6, and resuming the forward search from index 4 finds that same occurrence.

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

    String s = "key=value";
    String k = s.substring(0, s.indexOf("="));
    String v = s.substring(s.indexOf("=") + 1);
    System.out.print(v + k.length());
    • value3
    • value9
    • key3
    • value5
    Show worked solution

    Correct answer: value3

    The value is everything past the separator and the key has three characters.