Verityprep
Question Bank

AP® Computer Science A FRQ Practice: Free-Response Questions with Solutions

Work through AP Computer Science A free-response questions unit by unit, then check each answer against a worked solution.

Free · no sign-up Open questions need no account. Sign up or log in to open the rest.

Questions 171–180 of 329
  1. 171
  2. 172
  3. 173
  4. 174
  5. 175
  6. 176
  7. 177
  8. 178
  9. 179
  10. 180

Question 171

Free response Unit 3: Class Creation

A downtown parking garage charges a flat rate per hour for each car that parks. The garage has a fixed number of spaces. When a car enters, it occupies one space. When a car exits, the system calculates the charge based on the number of hours parked and adds it to the total revenue. A car cannot enter if no spaces are available. The following table shows an example sequence of operations on a ParkingGarage object.

STATEMENTS AND EXPRESSIONSVALUE RETURNEDCOMMENT
ParkingGarage g = new ParkingGarage(3, 5);Three spaces, $5 per hour
g.getOpenSpaces()3
g.enter()trueCar enters successfully
g.enter()trueSecond car enters
g.getOpenSpaces()1
g.enter()trueThird car enters
g.enter()falseGarage is full
g.exit(3)15Car parked 3 hours: 3 × 5 = 15
g.getOpenSpaces()1One space freed
g.exit(1)5Car parked 1 hour: 1 × 5 = 5
g.getRevenue()20Total revenue: 15 + 5

Write the complete ParkingGarage class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class ParkingGarage {
The answer and the full solution open in a window.

Question 172

Free response Unit 3: Class Creation
This question involves the implementation of a fitness tracking system that is represented by the StepTracker class. A StepTracker object is created with a…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 173

Free response Unit 3: Class Creation
Dice Record Game Write a class that models a dice game played in rounds. When an object is created, it receives an initial record score. This value represents…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 174

Free response Unit 3: Class Creation
Secret Word Guessing Game Write a class that models a secret-word guessing game. When an object is created, it receives a secret word. A player makes guesses…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 175

Free response Unit 3: Class Creation
Word Slot Game Write a class that stores a String containing words separated by single spaces. When an object is created, it receives this String. Example…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 176

Free response Unit 3: Class Creation
An arcade uses cards to keep track of tickets won by players. Write a class named ArcadeCard. Each ArcadeCard object should store: the player's name the current…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 177

Free response Unit 3: Class Creation

A fitness app records daily exercise sessions for one user. You will implement a class FitnessTracker that stores a rolling log of the user's most recent workouts and reports summary statistics. The class will be used like this.

FitnessTracker ft = new FitnessTracker(7);    // track the last 7 sessions
ft.logSession(30, 4.2);                       // 30 minutes, 4.2 kilometers
ft.logSession(45, 6.0);
ft.logSession(20, 2.5);
double avgPace = ft.averagePace();            // minutes per kilometer
int active     = ft.activeDays(25);           // sessions of at least 25 minutes

A FitnessTracker must meet the following specifications.

Method / Constructor HeaderDescription
public FitnessTracker(int capacity) Creates a tracker that retains at most capacity of the most recently logged sessions. The tracker begins with no logged sessions. You may assume capacity > 0.
public void logSession(int minutes, double kilometers) Records a single workout session. If the tracker already holds capacity sessions, the oldest logged session is discarded before the new session is added. minutes is always > 0 and kilometers is always > 0.0.
public int sessionCount() Returns the number of sessions currently stored (always between 0 and capacity).
public double averagePace() Returns the average pace (minutes per kilometer) across all currently stored sessions, computed as (total minutes) / (total kilometers) using double arithmetic. If no sessions are stored, returns 0.0.
public int activeDays(int minMinutes) Returns the number of currently stored sessions whose duration is greater than or equal to minMinutes.

Write the complete FitnessTracker class. Your implementation must meet all specifications and conform to the example. You may define additional private fields, constructors, and helper methods as needed. You may use any ArrayList- or array-based approach.

The answer and the full solution open in a window.

Question 178

Free response Unit 3: Class Creation

A small community library uses a single rack to display books available for checkout. The rack has a fixed physical capacity, and books sit on the rack in a definite left-to-right order. Patrons interact with the rack through four operations, and a librarian wants a LibraryRack class that supports them directly.

Books are identified by their titles, represented as String values. Two books with the same title are considered the same book — the rack never contains duplicate titles. Positions on the rack are 1-based: the leftmost book is at position 1.

Write the complete LibraryRack class. Your implementation must include the instance variables needed to support the behavior and the constructor and methods described below.

Class Specification

MemberDescription
public LibraryRack(int capacity)Constructs an empty rack with the given maximum capacity. You may assume capacity >= 1.
public boolean addBook(String title)Adds title to the right end of the rack and returns true. If the rack is already full, or a book with the same title is already on the rack, the method makes no change and returns false.
public String checkOut(int pos)Removes and returns the title at 1-based position pos. Books to the right of pos shift one position to the left to close the gap. If pos is not a valid position on the rack (including when the rack is empty), the method makes no change and returns null.
public boolean moveToFront(String title)If title is on the rack, removes it from its current position, inserts it at position 1 (all other books keeping their relative order), and returns true. If title is not on the rack, the method makes no change and returns false.
public int count()Returns the number of books currently on the rack.

Example

Consider the following sequence of calls on LibraryRack r = new LibraryRack(4);

CallReturnsRack afterward (left → right)
r.addBook("Emma")trueEmma
r.addBook("Dune")trueEmma, Dune
r.addBook("1984")trueEmma, Dune, 1984
r.addBook("Dune")falseEmma, Dune, 1984
r.moveToFront("1984")true1984, Emma, Dune
r.checkOut(2)"Emma"1984, Dune
r.checkOut(5)null1984, Dune
r.count()21984, Dune

Constraints

  • You must declare and use instance variables consistent with the behaviors above.
  • You must not use any String or ArrayList methods that are not in the AP Java Quick Reference.
  • Write the entire class, including necessary import statements if any.
The answer and the full solution open in a window.

Question 179

Free response Unit 3: Class Creation

A music teacher wants to track how students log their practice time. You will design a class PracticeLog that records each practice session's duration (in minutes) and answers summary queries.

A PracticeLog object supports the following behavior:

  • When constructed, a new log has zero minutes logged, zero sessions, and a longest-session value of 0.
  • The method logSession(int minutes) records a session of the given duration. If minutes is less than or equal to 0, the call is ignored (nothing is updated). Otherwise, the total minutes and session count increase, and the longest-session value is updated if this session is longer than any previously recorded session.
  • The method averageSession() returns the average number of minutes per recorded session, as a double. If no sessions have been logged, the method returns 0.0.
  • The method getTotal() returns the total minutes logged.
  • The method getCount() returns the number of logged sessions.
  • The method getLongest() returns the longest single session ever logged, or 0 if none.

The following table contains a sample code execution sequence and the corresponding results.

PracticeLog log = new PracticeLog();
log.logSession(30);
log.logSession(0);
log.logSession(45);
log.logSession(20);
log.getTotal();
log.getCount();
log.getLongest();
log.averageSession();
Statements and Expressions Value Returned (blank if no value) Comment
PracticeLog log = new PracticeLog(); Creates a new log with totalMinutes=0, sessionCount=0, longestSession=0.
log.logSession(30); Records a 30-minute session. total=30, count=1, longest=30.
log.logSession(0); Ignored (minutes \(\leq 0\)). No fields change. total=30, count=1, longest=30.
log.logSession(45); Records a 45-minute session. total=75, count=2, longest is updated to 45.
log.logSession(20); Records a 20-minute session. total=95, count=3. Longest stays at 45 (\(20 \lt 45\)).
log.getTotal(); 95 Returns current totalMinutes.
log.getCount(); 3 Returns current sessionCount (the ignored 0-minute call did not count).
log.getLongest(); 45 Returns the maximum single-session duration recorded so far.
log.averageSession(); 31.666666666666668 Returns 95 / 3 as a double. Multiplying by 1.0 in the computation avoids integer division.

Write the complete PracticeLog class, including all instance variables, the constructor, and all methods described above. Your implementation must conform to all specifications and must behave correctly for all valid inputs.

The answer and the full solution open in a window.

Question 180

Free response Unit 3: Class Creation
A charity kiosk uses a class DonationJar to track cash donations. Amounts are measured in whole cents and are always non-negative. The class must support adding…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 171 of 329
Page 18 of 33