Skip to main content

Unit 1 · Using Objects and Methods

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.

Which option matches the complete output produced by this segment?

public class StringIndexingContext
{
    public static void runExample()
    {
        String s = "signal";
        System.out.print(s.length() + " " + s.substring(0, 1));
    }

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