Out-of-bounds error in AP Computer Science A

In AP Computer Science A, an out-of-bounds error is a runtime exception (ArrayIndexOutOfBoundsException) thrown when code tries to access an array or string index outside the valid range of 0 to length - 1, such as a negative index or an index equal to the length.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is out-of-bounds error?

An out-of-bounds error happens when your code asks for an index that doesn't exist. Java arrays are zero-indexed, so an array of length 4 has valid indices 0, 1, 2, and 3. Ask for index 4 (or index -1) and Java throws an ArrayIndexOutOfBoundsException at runtime. The program compiles fine, runs fine up to that line, then crashes the moment the bad access happens.

This matters because of EK 4.3.A.2 in the CED. An array's length is fixed at creation and accessible through the length attribute, but length itself is not a valid index. It's one past the last one. The classic trap is arr[arr.length], which always crashes, while arr[arr.length - 1] correctly grabs the last element. Strings work the same way with their own version, StringIndexOutOfBoundsException, when methods like substring get an index past the end.

Why out-of-bounds error matters in AP® Computer Science A

Out-of-bounds errors live in Topic 4.3 (Array Creation and Access) in Unit 4: Data Collections, supporting learning objective 4.3.A, which asks you to develop code using 1D arrays. You can't write correct array code without knowing where the boundaries are, and the College Board knows it. A huge share of array MCQs are really boundary questions in disguise. They hand you a loop or an index expression and ask whether it runs cleanly, produces wrong output, or throws an exception. Knowing that valid indices run from 0 to length - 1, and being able to spot the exact line that steps outside that range, is one of the highest-value skills in all of Unit 4.

How out-of-bounds error connects across the course

The length attribute and array access (Unit 4)

Per EK 4.3.A.2, an array's length is fixed at creation and read through length. The out-of-bounds error is the flip side of that fact. Since indexing starts at 0, the last valid index is length - 1, which means arr[arr.length] is always a crash, never the last element.

Object reference (Unit 4)

Out-of-bounds errors aren't the only runtime crash in array code. If an array slot holds a reference type, its default value is null (EK 4.3.A.3), and calling a method on that null element throws a NullPointerException instead. Same crash-at-runtime behavior, different cause. One is a bad index, the other is a missing object.

Loop traversal of arrays (Unit 4)

Most out-of-bounds errors come from loop conditions, not single lines. Writing i <= arr.length instead of i < arr.length runs cleanly for every iteration except the last one, then blows up. When you trace a loop on the exam, check the final value of the index variable first.

String objects and methods (Unit 1)

Strings are indexed exactly like arrays, from 0 to length() - 1. Calling substring or similar methods with an index past the end throws a StringIndexOutOfBoundsException. Same concept, same mental model, different class.

Is out-of-bounds error on the AP® Computer Science A exam?

Out-of-bounds errors show up constantly in Unit 4 multiple-choice questions, usually disguised as "what does this code print?" A typical stem gives you a method like return arr[arr.length]; and asks for the result. The answer is never a value, it's an ArrayIndexOutOfBoundsException at runtime. Another common pattern adds up several array accesses where most are valid (vals[1], vals[vals.length - 1]) and one isn't (vals[vals.length]), testing whether you catch the single bad access. On FRQs, the error is tested in reverse. When you write array traversal code for Question 3, an i <= arr.length loop bound costs you points because graders treat the out-of-bounds access as broken code. Your job on MCQs is to spot the crash; your job on FRQs is to never cause one.

Out-of-bounds error vs Off-by-one error

An off-by-one error is a logic mistake where your loop runs one time too many or too few. An out-of-bounds error is the runtime exception that can result from one. They overlap but aren't the same. A loop that stops at length - 2 instead of length - 1 is off by one but never crashes, it just silently skips the last element. A loop that goes to length is off by one AND throws an out-of-bounds exception. On the exam, distinguish "wrong output" answer choices from "exception is thrown" answer choices using exactly this difference.

Key things to remember about out-of-bounds error

  • Valid indices for an array run from 0 to length - 1, so arr[arr.length] always throws an ArrayIndexOutOfBoundsException.

  • An out-of-bounds error is a runtime exception, not a compile-time error, so the code compiles and runs normally until the bad access actually executes.

  • Negative indices like arr[-1] are just as out-of-bounds as indices that are too large.

  • The most common cause on the exam is a loop condition using <= arr.length instead of < arr.length, which crashes on the final iteration.

  • Strings throw the same kind of error (StringIndexOutOfBoundsException) when methods like substring receive an index past the end.

  • An out-of-bounds access crashes the program at that line, so any code after it never runs.

Frequently asked questions about out-of-bounds error

What is an out-of-bounds error in AP Computer Science A?

It's a runtime exception (ArrayIndexOutOfBoundsException) thrown when code accesses an array or string index outside the valid range of 0 to length - 1. It's tested heavily in Unit 4, Topic 4.3 (Array Creation and Access).

Is an out-of-bounds error a compile-time error?

No. Java compiles arr[arr.length] without complaint because the compiler doesn't check index values. The error only appears at runtime, the moment the bad access executes. That's why MCQ answer choices say "an exception is thrown at runtime," not "the code does not compile."

Why does arr[arr.length] cause an error but arr[arr.length - 1] doesn't?

Arrays are zero-indexed, so an array of length 4 has indices 0 through 3. The length attribute (4) is one past the last valid index, while length - 1 (3) is exactly the last element. This single fact answers a surprising number of Unit 4 questions.

What's the difference between an out-of-bounds error and a NullPointerException?

An out-of-bounds error means the index itself is invalid, like arr[5] in a length-4 array. A NullPointerException means the index was fine but the element there is null, which happens with reference-type arrays since their elements default to null when created with new. Bad index versus missing object.

Does an out-of-bounds error happen with strings too?

Yes. Strings are indexed 0 to length() - 1 just like arrays, and methods like substring throw a StringIndexOutOfBoundsException when given an index past the end. The boundary rule is identical, only the class differs.