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 251–260 of 402
  1. 251
  2. 252
  3. 253
  4. 254
  5. 255
  6. 256
  7. 257
  8. 258
  9. 259
  10. 260

Question 251

Multiple choice Writing Classes
Consider this class. public class Fraction { private int num; private int den; public Fraction(int n, int d) { num = n; den = d; } public Fraction…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 252

Multiple choice Writing Classes
Consider the following class. public class GradeBook { private int[] scores; private int count; public GradeBook(int capacity) { scores = new int[capacity];…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 253

Multiple choice Writing Classes
Consider the following class. public class Temperature { private double celsius; public Temperature(double c) { celsius = c; } public void adjust(double delta)…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 254

Multiple choice Writing Classes
Consider the following class. public class Box { private int width; private int height; public Box(int w, int h) { width = w; height = h; } public int area() {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 255

Multiple choice Writing Classes
Consider the following class. public class Wallet { private int cash; public Wallet(int c) { cash = c; } public boolean transferTo(Wallet other, int amount) {…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 256

Free response Writing Classes
This question involves the implementation of a music playlist system that is represented by the Playlist class. A Playlist object is created with a parameter…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 257

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 258

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 259

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 260

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 251 of 402
Page 26 of 41