Verityprep
Question Bank · Unit 3

AP® Computer Science A Class Creation Multiple Choice Questions

Work through AP Computer Science A multiple-choice 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 241–250 of 359
  1. 241
  2. 242
  3. 243
  4. 244
  5. 245
  6. 246
  7. 247
  8. 248
  9. 249
  10. 250

Question 241

Multiple choice Writing Classes

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

public class GameTimer {
    private int seconds;
    private boolean running;
    public GameTimer(int s) { seconds = s; running = false; }
    public void start() { running = true; }
    public int tick(int n) {
        if (!running) { return seconds; }
        seconds -= n;
        if (seconds <= 0) { seconds = 0; running = false; }
        return seconds;
    }
}
// Client:
GameTimer gt = new GameTimer(30);
int t1 = gt.tick(5);
gt.start();
int t2 = gt.tick(12);
int t3 = gt.tick(10);
int t4 = gt.tick(15);
int t5 = gt.tick(5);
System.out.println(t1 + " " + t2 + " " + t3 + " " + (t4 + t5));
Pick an answer, then check it. The solution opens in a window.

Question 242

Multiple choice Writing Classes

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

public class Toggle {
    private boolean stateA;
    private boolean stateB;
    private int flips;
    public Toggle() { stateA = true; stateB = false; flips = 0; }
    public void flip() {
        boolean temp = stateA;
        stateA = stateB;
        stateB = temp;
        flips++;
    }
    public String status() {
        return stateA + " " + stateB + " " + flips;
    }
}
// Client:
Toggle t = new Toggle();
t.flip(); t.flip(); t.flip();
System.out.println(t.status());
Pick an answer, then check it. The solution opens in a window.

Question 243

Multiple choice Writing Classes

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

public class TextEditor {
    private String text;
    public TextEditor(String t) { text = t; }
    public void insertAt(int pos, String s) {
        text = text.substring(0, pos) + s + text.substring(pos);
    }
    public void deleteRange(int from, int to) {
        text = text.substring(0, from) + text.substring(to);
    }
    public String getText() { return text; }
}
// Client:
TextEditor te = new TextEditor("HELLO");
te.insertAt(5, " WORLD");
te.deleteRange(4, 6);
te.insertAt(4, "-");
System.out.println(te.getText() + " " + te.getText().length());
Pick an answer, then check it. The solution opens in a window.

Question 244

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 245

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 246

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 247

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 248

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 249

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 250

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 241 of 359
Page 25 of 36