AP CSA 4.4 Exercise 1: Both Directions

Unit 4: Data Collections · Lesson 4.4 · Exercise 1

Both Directions

Traversing Arrays. Write real Java, run it, and submit it to be graded against hidden test cases.

Why this one is worth doing

Sometimes you need the index and sometimes you only need the value. This exercise asks for both in the same program so the choice between an indexed loop and an enhanced one has to be made deliberately.

What to write

  1. Read an integer count, then that many integers into an array.
  2. Print every element on its own line, front to back.
  3. Print every element on its own line, back to front.
  4. Print the index of the largest element. When it appears more than once, print the FIRST index it appears at.
  5. Print the sum of the elements at even indexes.

Your program reads

An integer count of at least 1, then that many integers.

Your program prints

The elements forwards, then backwards, then the index of the maximum, then the sum at even indexes.

Worked examples

These are the cases you can see. There are more you cannot, and they use different values, so an answer that prints these numbers as constants will fail.

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

Your answer

Main.java

Input for the Run button

Run sends whatever is in the input box below. Submitting runs your program against every test case, including hidden ones with different input.


  

  

Stuck?

Hint 1

The backwards loop starts at length - 1 and tests i >= 0. Starting at length is the off by one that throws.

Hint 2

Use a strict > when hunting the maximum index, so a repeated maximum keeps the first position rather than the last.

Hint 3

Even INDEXES means 0, 2, 4 and so on, which is not the same as even values. Step the loop by 2.

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]