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.

Questions 231–240 of 359
  1. 231
  2. 232
  3. 233
  4. 234
  5. 235
  6. 236
  7. 237
  8. 238
  9. 239
  10. 240

Question 231

Multiple choice Writing Classes

Consider the following class and client code.

public class Tank {
    private int level;
    private int capacity;
    public Tank(int cap) { capacity = cap; level = 0; }
    public int fill(int amount) {
        int space = capacity - level;
        if (amount <= space) { level += amount; return 0; }
        else { level = capacity; return amount - space; }
    }
    public int drain(int amount) {
        if (amount <= level) { level -= amount; return amount; }
        else { int drained = level; level = 0; return drained; }
    }
    public int getLevel() { return level; }
}
// Client:
Tank t1 = new Tank(50);
Tank t2 = new Tank(30);
int overflow = t1.fill(45);
overflow += t1.fill(20);
int got = t1.drain(overflow);
t2.fill(got);
System.out.println(t1.getLevel() + " " + t2.getLevel());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 232

Multiple choice Writing Classes

Consider the following class and client code.

public class Inventory {
    private ArrayList<Integer> quantities;
    public Inventory() { quantities = new ArrayList<Integer>(); }
    public void addItem(int qty) { quantities.add(qty); }
    public int removeSmall(int threshold) {
        int count = 0;
        for (int i = quantities.size() - 1; i >= 0; i--) {
            if (quantities.get(i) < threshold) {
                quantities.remove(i);
                count++;
            }
        }
        return count;
    }
    public int getSize() { return quantities.size(); }
}
// Client:
Inventory inv = new Inventory();
inv.addItem(5); inv.addItem(12); inv.addItem(3);
inv.addItem(8); inv.addItem(1);
int removed = inv.removeSmall(5);
System.out.println(removed + " " + inv.getSize());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 233

Multiple choice Writing Classes

Consider the following class and client code.

public class Sequence {
    private int current;
    private int step;
    public Sequence(int start, int s) { current = start; step = s; }
    public int next() { current += step; return current; }
    public int skip(int n) {
        for (int i = 0; i < n; i++) { next(); }
        return current;
    }
}
// Client:
Sequence seq = new Sequence(10, 3);
int a = seq.next();
int b = seq.skip(4);
int c = seq.next();
System.out.println(a + " " + b + " " + c);

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 234

Multiple choice Writing Classes

Consider the following class and client code.

public class Account {
    private int balance;
    public Account(int b) { balance = b; }
    public int transfer(Account to, int amount) {
        int actual = amount;
        if (amount > balance) {
            actual = balance;
        }
        balance -= actual;
        to.balance += actual;
        return actual;
    }
    public int getBalance() { return balance; }
}
// Client:
Account a = new Account(100);
Account b = new Account(50);
Account c = new Account(20);
int t1 = a.transfer(b, 70);
int t2 = b.transfer(c, 200);
int t3 = c.transfer(a, 40);
System.out.println(a.getBalance() + " " + b.getBalance() + " " + c.getBalance());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 235

Multiple choice Writing Classes

Consider the following class and client code.

public class TextEditor {
    private String text;
    public TextEditor(String t) { text = t; }
    public String replaceFirst(String target, String replacement) {
        int pos = text.indexOf(target);
        if (pos == -1) return text;
        text = text.substring(0, pos) + replacement
             + text.substring(pos + target.length());
        return text;
    }
    public String getText() { return text; }
}
// Client:
TextEditor ed = new TextEditor("abcabcabc");
ed.replaceFirst("bc", "X");
ed.replaceFirst("bc", "Y");
System.out.println(ed.getText());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 236

Multiple choice Writing Classes

Consider the following class and client code.

public class Stats {
    private int[] data;
    public Stats(int[] d) {
        data = new int[d.length];
        for (int i = 0; i < d.length; i++) data[i] = d[i];
    }
    public int median() {
        if (data.length % 2 == 1) return data[data.length / 2];
        return (data[data.length / 2 - 1] + data[data.length / 2]) / 2;
    }
}
// Client:
int[] values = {2, 5, 7, 10, 12, 15};
Stats s = new Stats(values);
System.out.println(s.median());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 237

Multiple choice Writing Classes

Consider the following class and client code.

public class ScoreTracker {
    private int best;
    private int worst;
    private int total;
    private int attempts;
    public ScoreTracker(int firstScore) {
        best = firstScore; worst = firstScore;
        total = firstScore; attempts = 1;
    }
    public void recordScore(int score) {
        attempts++; total += score;
        if (score > best) best = score;
        if (score < worst) worst = score;
    }
    public int range() { return best - worst; }
    public int adjustedTotal() { return total - best - worst; }
}
// Client:
ScoreTracker st = new ScoreTracker(70);
st.recordScore(85); st.recordScore(60);
st.recordScore(90); st.recordScore(75);
System.out.println(st.range() + " " + st.adjustedTotal());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 238

Multiple choice Writing Classes

Consider the following class and client code.

public class Ratio {
    private int top;
    private int bottom;
    public Ratio(int t, int b) { top = t; bottom = b; }
    public Ratio add(Ratio other) {
        int newTop = top * other.bottom + other.top * bottom;
        int newBottom = bottom * other.bottom;
        return new Ratio(newTop, newBottom);
    }
    public String toString() { return top + "/" + bottom; }
}
// Client:
Ratio r1 = new Ratio(1, 3);
Ratio r2 = new Ratio(1, 4);
System.out.println(r1.add(r2));

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 239

Multiple choice Writing Classes

Consider the following class and client code.

public class Light {
    private boolean on;
    public Light() { on = false; }
    public void toggle() { on = !on; }
    public boolean isOn() { return on; }
}
// Client:
Light bulb = new Light();
for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0 || i % 3 == 0) {
        bulb.toggle();
    }
}
System.out.println(bulb.isOn());

What is printed?

Pick an answer, then check it. The solution opens in a window.

Question 240

Multiple choice Writing Classes

Consider the following class and client code.

public class Vector {
    private int x;
    private int y;
    public Vector(int x, int y) { this.x = x; this.y = y; }
    public Vector add(Vector other) {
        return new Vector(x + other.x, y + other.y);
    }
    public int dot(Vector other) { return x * other.x + y * other.y; }
    public String toString() { return "(" + x + "," + y + ")"; }
}
// Client:
Vector a = new Vector(3, 4);
Vector b = new Vector(-1, 2);
Vector c = a.add(b);
System.out.println(c + " " + a.dot(b));

What is printed?

Pick an answer, then check it. The solution opens in a window.
Question 231 of 359
Page 24 of 36