Verityprep
Question Bank · Unit 3

AP® Computer Science A Class Creation Practice Questions

Work through AP Computer Science A practice 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.

Subtopic: Writing Classes Clear filter

Questions 361–370 of 402
  1. 361
  2. 362
  3. 363
  4. 364
  5. 365
  6. 366
  7. 367
  8. 368
  9. 369
  10. 370

Question 361

Multiple choice Writing Classes
Consider the following class.
public class Account
{
    private int id;
    private double balance;
    private boolean active;
    private String owner;

    public Account(int theId)
    {
        id = theId;
    }

    public String describe()
    {
        return id + " " + balance + " " + active + " " + owner;
    }
}
Consider the following code segment.
Account a = new Account(42);
System.out.println(a.describe());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 362

Multiple choice Writing Classes
Consider the following class. public class Token { private static int issued = 0; private int id; public Token() { issued = issued + 1; id = issued; } public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 363

Multiple choice Writing Classes
Consider the following class. public class Pet { private String name; private int age; public Pet(String name, int age) { name = name; this.age = age; } public…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 364

Multiple choice Writing Classes
A programmer is designing a class Score intended to represent a numeric grade with both an int form and a double form. Consider the four method declarations…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 365

Multiple choice Writing Classes
Consider the following class.
public class Money
{
    private int cents;

    public Money(int c) { cents = c; }
    public String toString()
    {
        return "$" + (cents / 100) + "." + (cents % 100);
    }
}
Consider the following code segment.
Money m = new Money(2507);
System.out.println("price: " + m);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 366

Multiple choice Writing Classes
Consider the following class. public class Bag { private int items; public Bag(int n) { items = n; } public int getItems() { return items; } public void add(int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 367

Multiple choice Writing Classes
Consider the following two classes, written in the same package. public class Wallet { private int coins; public Wallet(int c) { coins = c; } public int…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 368

Multiple choice Writing Classes
Consider the following class. public class Snapshot { private int[] values; public Snapshot(int[] data) { values = data; } public int at(int i) { return…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 369

Multiple choice Writing Classes
Consider the following class.
public class Stat
{
    private int total;
    private int count;

    public Stat() { total = 0; count = 0; }

    public void record(int v)
    {
        total = total + v;
        count = count + 1;
    }

    public double mean()
    {
        if (count == 0) { return 0.0; }
        return total * 1.0 / count;
    }

    public void reset() { total = 0; count = 0; }
}
Consider the following code segment.
Stat s = new Stat();
s.record(2);
s.record(4);
double a = s.mean();
s.reset();
s.record(10);
double b = s.mean();
System.out.println(a + " " + b);
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 370

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 361 of 402
Page 37 of 41