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.

Questions 271–280 of 402
  1. 271
  2. 272
  3. 273
  4. 274
  5. 275
  6. 276
  7. 277
  8. 278
  9. 279
  10. 280

Question 271

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class Accumulator {
    private int total;
    private int count;
    private int limit;
    public Accumulator(int lim) { total = 0; count = 0; limit = lim; }
    public boolean add(int val) {
        if (total + val > limit) { return false; }
        total += val;
        count++;
        return true;
    }
    public int getAverage() {
        if (count == 0) { return 0; }
        return total / count;
    }
}
// Client:
Accumulator acc = new Accumulator(50);
acc.add(15);
acc.add(20);
acc.add(10);
boolean b = acc.add(8);
System.out.println(acc.getAverage() + " " + b);
Pick an answer, then check it. The solution opens in a window.

Question 272

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class Warehouse {
    private int stock;
    private int capacity;
    private int totalShipped;
    public Warehouse(int cap) { stock = 0; capacity = cap; totalShipped = 0; }
    public int receive(int qty) {
        int space = capacity - stock;
        if (qty <= space) { stock += qty; return 0; }
        stock = capacity;
        return qty - space;
    }
    public int ship(int qty) {
        if (qty <= stock) { stock -= qty; totalShipped += qty; return qty; }
        int shipped = stock;
        totalShipped += stock;
        stock = 0;
        return shipped;
    }
    public int getTotalShipped() { return totalShipped; }
}
// Client:
Warehouse w = new Warehouse(100);
int r1 = w.receive(80);
int r2 = w.receive(30);
int s1 = w.ship(60);
int s2 = w.ship(50);
System.out.println(r1 + r2 + " " + (s1 + s2) + " " + w.getTotalShipped());
Pick an answer, then check it. The solution opens in a window.

Question 273

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class ScoreTracker {
    private int highest;
    private int lowest;
    private int count;
    public ScoreTracker() { highest = 0; lowest = 100; count = 0; }
    public void addScore(int s) {
        if (s > highest) { highest = s; }
        if (s < lowest) { lowest = s; }
        count++;
    }
    public int getRange() {
        if (count == 0) { return 0; }
        return highest - lowest;
    }
}
// Client:
ScoreTracker st = new ScoreTracker();
st.addScore(72); st.addScore(85);
st.addScore(60); st.addScore(93);
st.addScore(60);
System.out.println(st.getRange());
Pick an answer, then check it. The solution opens in a window.

Question 274

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class NumberChain {
    private int value;
    public NumberChain(int v) { value = v; }
    public int doubleIt() { value *= 2; return value; }
    public int addTo(int n) { value += n; return value; }
    public int modBy(int n) { value = value % n; return value; }
}
// Client:
NumberChain nc = new NumberChain(7);
int a = nc.doubleIt();
int b = nc.addTo(a);
int c = nc.modBy(5);
int d = nc.doubleIt();
System.out.println(a + b + c + d);
Pick an answer, then check it. The solution opens in a window.

Question 275

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class Counter {
    private int value;
    private int resetCount;
    public Counter(int start) { value = start; resetCount = 0; }
    public void increment(int n) { value += n; }
    public void reset() { value = 0; resetCount++; }
    public int getResetCount() { return resetCount; }
    public int getValue() { return value; }
}
// Client:
Counter c = new Counter(10);
c.increment(5);
c.reset();
c.increment(3);
c.increment(7);
c.reset();
c.increment(1);
System.out.println(c.getValue() + " " + c.getResetCount());
Pick an answer, then check it. The solution opens in a window.

Question 276

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class PasswordValidator {
    private int minLength;
    private String required;
    public PasswordValidator(int min, String req) { minLength = min; required = req; }
    public int validate(String password) {
        int score = 0;
        if (password.length() >= minLength) { score += 2; }
        if (password.indexOf(required) != -1) { score += 3; }
        if (password.length() >= minLength * 2) { score += 1; }
        return score;
    }
}
// Client:
PasswordValidator pv = new PasswordValidator(4, "xyz");
int s1 = pv.validate("abc");
int s2 = pv.validate("abcxyz12");
int s3 = pv.validate("longpassword");
System.out.println(s1 + " " + s2 + " " + s3);
Pick an answer, then check it. The solution opens in a window.

Question 277

Multiple choice Writing Classes

Consider the following class and client code. What is printed?

public class BankAccount {
    private int balance;
    private int transactions;
    public BankAccount(int initial) { balance = initial; transactions = 0; }
    public void deposit(int amount) { balance += amount; transactions++; }
    public int withdraw(int amount) {
        int fee = 0;
        if (transactions >= 3) { fee = 5; }
        if (amount + fee <= balance) {
            balance -= (amount + fee);
            transactions++;
            return amount;
        }
        return 0;
    }
    public int getBalance() { return balance; }
}
// Client:
BankAccount acct = new BankAccount(100);
acct.deposit(50);
acct.deposit(30);
int w1 = acct.withdraw(20);
int w2 = acct.withdraw(50);
int w3 = acct.withdraw(110);
System.out.println(acct.getBalance() + " " + (w1 + w2 + w3));
Pick an answer, then check it. The solution opens in a window.

Question 278

Free response Writing Classes

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 279

Multiple choice Writing Classes
Consider the following classes. public class TextTools { public String repeatFirst(String word) { return word.substring(0,1) + word; } } public class…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 280

Multiple choice Writing Classes
Consider the following classes. public class CodeTools { public String merge(String a, String b) { return a.substring(0,1) + b.substring(0,1) +…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 271 of 402
Page 28 of 41