Out of Subset Responses and the 2025 Free Response Questions
The APCSA exam for 2025 was administered on May 7th. The free response questions were released to AP Central and can be found here.
All of these questions can be solved within the AP Java subset; however, there are other ways to solve these problems that are outside of the Java subset. Some of these solutions use newer Java features, such as records and the use of methods in the SequencedCollection class. In this article, we will showcase solutions that use out of subset content.
Free Response Question 1: Dog Walker
Part A - In this solution, we will use var and the ternary conditional operator, both of which are outside the AP Java Subset.
public int walkDogs(int hour) {
var numDogAvail = company.numAvailableDogs(hour);
var dogsWalked = (numDogAvail >= maxDogs) ? maxDogs : numDogAvail;
company.updateDogs(hour, dogsWalked);
return dogsWalked;
}
While this solution is acceptable, the use of var
with numDogAvail
is not recommended as it is less readable when used to hold the return value of a method. We are not sure what the return type of numAvailableDogs
is without also referencing this method. In this case, we can guess that the value returned is an int
, but again, it isn't intuitive.
Rather than using an if
statement to check and see if the numDogAvail
is greater than maxDogs
, we can use a ternary conditional operator and assign dogsWalked
to the appropriate value based on this condition.
Part B - In this solution, we will also be using var and the ternary conditional operator.
public int dogWalkShift (int startHour, int endHour) {
var amountEarned = 0;
for (var h = startHour; h <= endHour; h++) {
var numDogs = walkDogs(h);
amountEarned += ((h >= 9 && h <= 17) || numDogs == maxDogs)
? numDogs * 5 + 3 : numDogs * 5;
}
}
In this case, amountEarned
and h
are excellent candidate for the use of var
. The numDogs
variable can use var
, but again since it is holding the value from a method it isn't the best use of var
.
The if
statement to determine amountEarned
can also be replaced by a ternary conditional operator.
Free Response Question 2: SignedText
The SignedText
class can be written as a record, which is a type of class. It is an excellent candidate for a record, since the first name and last name are not expected to change when using this class. Using a record in this case means that the instance variables and constructor for the class are created automatically. Students still need to write all the logic for the getSignature
and addSignature
methods.
public record SignedText(String firstName, String lastName) {
public String getSignature() {
return (firstName.length() == 0) ? lastName
: firstName.substring(0, 1) + "-" + lastName;
}
public String addSignature (String memo) {
var signature = getSignature();
var index = memo.indexOf(signature);
return (index == -1) ? memo + signature
: ((index == 0)
? (memo.substring(signature.length()) + signature)
: memo);
}
}
This solution also made use of var
and the ternary conditional operator.
In the method getSignature
, the ternary conditional operator can be used to create the signature under the condition of whether or not firstName
is an empty string or not.
In the method addSignature
, var
is being used for the return values for a call to getSignature
and indexOf
method. These are both cases where var can be used. The method getSignature
is within this class, so it isn't too difficult to figure out the return type and indexOf
is a common method for String
. But these are still less intuitive cases and for readability sake, not the best candidates for var
.
In addSignature
, we are also using the ternary conditional operator. This time it is nested. There is the check for whether signature
is in memo
and, if it is, a check to see if signature
appears at the beginning of the memo
.
Free Response Question 3: Competition
Part A - You can certainly use var in this solution.
Part B - In this solution, you will see var, ternary conditional operator and the use of methods from SequencedCollection class.
public ArrayList<Match> buildMatches(){
var copy = new ArrayList<Compet>();
var retMatches = new ArrayList<Match>();
for (var c: competitorList){
copy.add(c);
}
var first = (copy.size() % 2 == 1) ? copy.removeFirst()
: copy.getFirst();
while (copy.size() != 0) {
var match = new Match (copy.removeFirst(), copy.removeLast());
retMatches.add(match);
}
return retMatches;
}
The uses of var
for copy
, retMatches
, c
, and match
are all really good choices. Using it for first
is less intuitive since it is the return value of a method.
To use the remove
methods of the SequencedCollection
class, we need to make a copy of the ArrayList
so we don't destroy the original set of data. Once this is complete, we can remove the first element if the ArrayList
has an odd number of elements. And then match up the first and last elements using a while
loop and removing the ones that have been matched from the list with the removeFirst
and removeLast
methods.
This solution also uses the ternary conditional operator to remove the first element. However, since there was no change to the ArrayList
if the length was already even, we had to do something arbitrary that also didn't impact the solution. That is why we used getFirst
in that case. An if statement would have been cleaner in this case.
Free Response Question #4: SumorSamePuzzle
This FRQ can leverage var
in the solutions similar to the questions above.
Disclaimer
It is not recommended for students to write solutions that are outside the AP Java Subset as the questions are written with the subset in mind. Students should be wary of writing the solutions above on the exam, since readers are trained on in subset solutions. Readers do their very best to not penalize students for correct solutions, but if a reader doesn't recognize the code because it is unexpected, it could be scored incorrectly.
Hoping all your students scored a 5!