AP CSA 4.3 Debugging: One Past the End

Unit 4: Data Collections · Lesson 4.3 · Debugging

One Past the End

Array Creation and Access. This is not a blank editor: it is someone else's attempt. Find what is wrong, fix it, and submit to be graded against hidden test cases.

Why debugging is its own skill

Valid indexes run from 0 to length minus 1, which means length itself is always one step too far. This program crashes on that boundary and, once it stops crashing, still builds an array one slot too small to hold everything it was given.

What is wrong with it

  1. The printing loop uses i <= data.length, so its last pass reads an index that does not exist and throws ArrayIndexOutOfBoundsException. Fix the bound.
  2. The array is allocated with new int[n - 1], which is one slot short, so the last value read has nowhere to go. Allocate exactly n slots.
  3. The last-element line at the bottom is written correctly. Notice how it computes the final index and use the same reasoning on the loop.

The program reads

A count, then that many integers.

The program should print

Every element on its own line, then the length of the array, then the last element.

Worked examples

These show what the FIXED program should print. There are more cases you cannot see, and they use different values, so patching around just these numbers will fail.

Example 1 input
3
4 5 6
Example 1 output
4
5
6
3
6
Example 2 input
1
9
Example 2 output
9
1
9

Your answer

Main.java

Input for the Run button

Run sends whatever is in the code box below, bugs and all, so you can see the crash or the wrong answer for yourself before you fix anything.


  

  

Stuck?

Hint 1

An array of length 3 has indexes 0, 1 and 2. There is no index 3. Any loop written with <= against length asks for that missing slot on its final pass.

Hint 2

new int[n - 1] creates one fewer slot than requested. The reading loop is bounded by data.length, so it silently reads one value fewer than the input provides and the last number is left unread.

Hint 3

length is a field on an array and has no parentheses. length() with parentheses is the String method. Mixing them up is a compile error rather than a silent bug, which is one of the few mercies here.

Where to go next

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]