Verityprep
Question Bank · Unit 3

AP® Computer Science A Class Creation FRQ

Work through AP Computer Science A free-response questions on Class Creation, 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 31–40 of 43
  1. 31
  2. 32
  3. 33
  4. 34
  5. 35
  6. 36
  7. 37
  8. 38
  9. 39
  10. 40

Question 31

Free response Writing Classes
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 32

Free response Writing Classes
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 33

Free response Writing Classes
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 34

Free response Writing Classes

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 35

Free response Writing Classes

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 36

Free response Writing Classes

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 37

Free response Writing Classes
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 38

Free response Writing Classes

A small attended car park uses a class ParkingLot to manage its operation. The lot has a fixed total number of spaces, set when the lot opens for the day. The class must support recording cars entering and leaving the lot, reporting capacity information, and reporting how many cars have been admitted across the entire shift.

A ParkingLot object keeps track of three pieces of information:

  • the total number of spaces in the lot, set at construction and never changed afterward,
  • the current number of cars parked in the lot,
  • the total number of cars that have ever been admitted into the lot during the shift (a car that enters and later leaves still counts here).

Write the complete ParkingLot class. Your class must include the instance variables described above and the behaviors specified below. All instance variables must be private.

Constructor

public ParkingLot(int spaces) — Constructs a new lot with the given number of total spaces. The number of cars currently parked and the total ever admitted are both 0. You may assume spaces > 0.

Method: enter

public boolean enter() — Attempts to admit one car. If the lot is full (the current count equals the total number of spaces), no instance variable changes and the method returns false. Otherwise the current count increases by 1, the total ever admitted increases by 1, and the method returns true.

Method: leave

public boolean leave() — Attempts to release one car. If the lot is empty (the current count is 0), no instance variable changes and the method returns false. Otherwise the current count decreases by 1 and the method returns true. The total ever admitted is never decreased by leave.

Method: available

public int available() — Returns the number of spaces still free in the lot, computed from the total number of spaces and the current count.

Method: served

public int served() — Returns the total number of cars that have been admitted during the shift.

Method: occupancy

public double occupancy() — Returns the fraction of the lot currently occupied, expressed as a value in the range 0.0 to 1.0. The fraction is computed as the current number of cars divided by the total number of spaces. Because spaces > 0 by construction, no division-by-zero check is required.

The table below shows a possible sequence of calls on a newly constructed ParkingLot named lot. Use this sequence to guide the design and behavior of your class.

Statements and Expressions Value Returned (blank if no value) Comment
ParkingLot lot = new ParkingLot(3); Creates a new lot with 3 spaces, all empty, no one served yet.
lot.available() 3 All spaces are free.
lot.enter() true 1 car parked, served count becomes 1.
lot.enter() true 2 cars parked, served count becomes 2.
lot.enter() true 3 cars parked, served count becomes 3; lot is now full.
lot.enter() false Lot is full; rejection does not affect any field.
lot.available() 0 No spaces remain.
lot.occupancy() 1.0 Three of three spaces are occupied.
lot.leave() true 2 cars parked; served count is unchanged.
lot.leave() true 1 car parked.
lot.leave() true 0 cars parked; lot is now empty.
lot.leave() false Lot is already empty; rejection does not affect any field.
lot.served() 3 Total ever admitted across the shift is preserved.
lot.occupancy() 0.0 Zero of three spaces are occupied.

Write the complete ParkingLot class, including instance variables, constructor, and all methods described above.

The answer and the full solution open in a window.

Question 39

Free response Writing Classes
A small theater sells same-day tickets at a single service desk. Customers wait in a line, and the theater wants a TicketLine class that stores the names of…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 40

Free response Writing Classes

A school activity office keeps a line of students waiting to board a shuttle for an event. A BoardingLine object stores the names of students currently waiting.

A BoardingLine object is created with a maximum capacity. The class supports the following behavior:

  • addStudent adds a student name to the end of the line if the line is not full and the name is not already present. The method returns true if the name is added and false otherwise.
  • boardNext removes and returns the name at the front of the line. If the line is empty, the method returns null.
  • lineSize returns the number of students currently waiting.

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

Statements and Expressions Value Returned (blank if no value) Comment
BoardingLine line = new BoardingLine(3); Creates a new line that can hold at most three students.
line.lineSize(); 0 No students are waiting yet.
line.boardNext(); null There is nobody to board.
line.addStudent("Ava"); true "Ava" is added to the end of the line.
line.addStudent("Noah"); true The line now contains "Ava", "Noah", in that order.
line.addStudent("Ava"); false Duplicate names are not added.
line.addStudent("Mia"); true The line reaches capacity.
line.addStudent("Leo"); false The line is full, so "Leo" is not added.
line.boardNext(); "Ava" The front student is removed and returned.
line.lineSize(); 2 The remaining students are "Noah" and "Mia".

Write the complete BoardingLine class, including the constructor, instance variables, 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 31 of 43
Page 4 of 5