AP CSA 4.4 FRQ Practice: Forwards and Backwards

Unit 4: Data Collections · Lesson 4.4 · FRQ Practice

Forwards and Backwards

A free response question in the shape the exam uses: a stated contract, four rubric parts, and no main method handed to you. Worth 4 points.

Why this question is worth four points

A backwards traversal starts at length - 1 and a forwards one stops before length, and writing either bound the other way round is the crash this topic is famous for. An enhanced for loop avoids both, at the cost of not knowing where it is.

What you are given

The driver builds an int array and passes it to your methods. Write a class named Traverse. Do not write a main method.

The question

  1. (a) public static String forwards(int[] data) returns every element separated by single spaces, or the empty String for an empty array.
  2. (b) public static String backwards(int[] data) returns every element in reverse, separated by single spaces.
  3. (c) public static int sumEvenIndexes(int[] data) returns the sum of the elements at indexes 0, 2, 4 and so on.
  4. (d) public static int countMatching(int[] data, int target) returns how many elements equal target.

What the reader is looking for

  1. Write class Traverse with the four static methods described. No main method.
  2. Parts (a) and (b) put a single space BETWEEN elements and none at the start.
  3. Part (c) is about the INDEX being even, not the value.

Worked examples

These show what a correct answer prints. There are more cases you cannot see, and they use different values, so an answer built around just these numbers will fail.

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

Your answer

Main.java

Input for the Run button

How this is scored: your answer runs against every test case, and the fraction it passes becomes your score out of 4. That is not how a human AP reader marks a rubric, so treat the score as a check on whether your code works, and the rubric above as the thing you are actually practising.


  

  

Stuck?

Hint 1

A backwards loop starts at data.length - 1 and continues while the index is at least 0. Starting at data.length throws on the very first pass.

Hint 2

Part (c) steps the index by 2. The elements at odd indexes are skipped entirely, whatever their values.

Hint 3

The separator goes before every element except the one that comes first in the output, which is a different element in part (a) and part (b).

Before you submit

4 mistake(s) that lose points on this question

Each of these is a real error the grader catches. Check your answer against them before you submit, not instead of trying.

  • part (b) starts at length, one past the end
  • part (c) sums the even VALUES rather than the even indexes
  • part (a) stops one element short
  • part (d) counts every element rather than the matches

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]