AP CSA 4.5 FRQ Practice: Which One Won

Unit 4: Data Collections · Lesson 4.5 · FRQ Practice

Which One Won

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

This is where the tie breaking rule finally becomes gradeable. Returning the VALUE of the largest element hides whether you kept the first or the last one; returning its INDEX does not. A strict greater-than keeps the earlier index and a greater-or-equal keeps the later one, and the exam always says which it wants.

What you are given

The driver builds an int array and passes it to your methods. Every array has at least one element. Write a class named Find. Do not write a main method.

The question

  1. (a) public static int indexOfMax(int[] data) returns the index of the largest value, and the SMALLEST such index when it appears more than once.
  2. (b) public static int indexOfLastMax(int[] data) returns the LARGEST index at which the largest value appears.
  3. (c) public static int indexOf(int[] data, int target) returns the first index holding target, or -1 when it is absent.
  4. (d) public static boolean isSorted(int[] data) returns whether the values never decrease from left to right.

What the reader is looking for

  1. Write class Find with the four static methods described. No main method.
  2. Parts (a) and (b) differ ONLY in their tie breaking. One uses a strict comparison and the other does not.
  3. A one element array is sorted, and so is an array whose values are all equal.

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
4
Example 1 output
1
3
2
false
Example 2 input
4
1 2 2 5
2
Example 2 output
3
3
1
true

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

Track the INDEX of the best element rather than its value. Comparing data[i] to data[best] keeps both facts in one variable.

Hint 2

A strict > never replaces on a tie, so the earliest winner survives. A >= replaces every time, so the latest one does.

Hint 3

Part (d) compares each element to the one before it. Values that are equal do not break the ordering, so the test is < and not <=.

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 (a) replaces on a tie, so it reports the LAST maximum
  • part (b) does not replace on a tie, so it reports the FIRST maximum
  • part (c) returns the LAST match instead of the first
  • part (d) rejects an array with equal neighbours

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]