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 181–190 of 329
  1. 181
  2. 182
  3. 183
  4. 184
  5. 185
  6. 186
  7. 187
  8. 188
  9. 189
  10. 190

Question 181

Free response Unit 3: Class Creation

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 182

Free response Unit 3: Class Creation
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 183

Free response Unit 3: Class Creation

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 184

Free response Unit 3: Class Creation
The TemperatureGrid class stores a rectangular grid of integer temperature readings. public class TemperatureGrid { private int[][] temps; public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 185

Free response Unit 3: Class Creation

A ReadingTracker object records pages read toward a personal reading goal. A newly constructed tracker is given a positive page goal. The tracker starts with 0 pages read and 0 recorded reading days.

The method read takes a number of pages. If the argument is positive, those pages are added to the total pages read and the number of recorded reading days increases by one. If the argument is not positive, the tracker is unchanged. The method pagesLeft returns the number of additional pages needed to reach the goal, or 0 if the goal has already been reached. The method averagePages returns the average number of pages read per recorded reading day, or 0.0 if there have been no recorded reading days. The method metGoal returns whether the total pages read is at least the goal.

Statements and Expressions Value Returned (blank if no value) Comment
ReadingTracker rt = new ReadingTracker(120);A tracker with goal 120 is created.
rt.pagesLeft()120No pages have been recorded.
rt.read(35);Total pages becomes 35; recorded days becomes 1.
rt.read(0);Non-positive input is ignored.
rt.read(50);Total pages becomes 85; recorded days becomes 2.
rt.averagePages()42.5The average is 85 * 1.0 / 2.
rt.metGoal()falseThe goal has not yet been reached.
rt.read(40);Total pages becomes 125; recorded days becomes 3.
rt.pagesLeft()0No more pages are needed.
rt.metGoal()trueThe total is at least the goal.

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

The answer and the full solution open in a window.

Question 186

Free response Unit 3: Class Creation
Car rental companies rent cars to customers. You will write the complete RentalCarPlan class, which represents an agreement between a car rental company and a…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 187

Free response Unit 4: Data Collections
The factory keeps track of its printing machines in array machines, and it keeps track of its paper supply in two lists: newRolls to hold fresh rolls and…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 188

Free response Unit 4: Data Collections
Customers are ordered alphabetically by name, then by positive integer ID when names match. Matching names and IDs identify equal customers. Complete both…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 189

Free response Unit 4: Data Collections
The PR2004 is a robot that automatically gathers toys and other items scattered in a tiled hallway. A tiled hallway has a wall at each end and consists of a…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 190

Free response Unit 4: Data Collections
This question involves reasoning about an array of integers with no duplicate values. An inversion is a pair of values in an array where a larger value appears…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 181 of 329
Page 19 of 33