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 261–270 of 402
  1. 261
  2. 262
  3. 263
  4. 264
  5. 265
  6. 266
  7. 267
  8. 268
  9. 269
  10. 270

Question 261

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 262

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 263

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 264

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 265

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 266

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 267

Free response Writing Classes

This question involves the implementation of a vending machine system that is represented by the VendingMachine class. A VendingMachine object is created with two parameters: the price of the item in cents and the initial number of items in stock.

The VendingMachine class provides a constructor and the following methods.

  • purchase, which attempts a purchase and returns the change in cents, or -1 if the purchase fails
  • restock, which adds items to the stock
  • getRevenue, which returns the total revenue collected in cents
  • getStock, which returns the number of items remaining

The following table contains a sample code execution sequence and the corresponding results.

STATEMENTS AND EXPRESSIONSVALUE RETURNEDCOMMENT
VendingMachine vm = new VendingMachine(150, 3);Price is 150 cents per item. 3 items in stock.
vm.getStock()3
vm.getRevenue()0No purchases have been made.
vm.purchase(200)50Paid 200, price is 150, change is 50.
vm.getStock()2One item sold.
vm.getRevenue()150Revenue from one sale.
vm.purchase(100)-1Payment of 100 is less than price of 150.
vm.getStock()2No change, purchase failed.
vm.purchase(150)0Exact change, no money returned.
vm.purchase(200)50
vm.getStock()0All items sold.
vm.getRevenue()450Three successful purchases.
vm.purchase(200)-1Out of stock.
vm.getStock()0
vm.restock(2)Two items added.
vm.getStock()2
vm.purchase(175)25
vm.getRevenue()600Four total successful purchases.

Write the complete VendingMachine class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.

public class VendingMachine {

}
The answer and the full solution open in a window.

Question 268

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 269

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 270

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 261 of 402
Page 27 of 41