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 311–320 of 402
  1. 311
  2. 312
  3. 313
  4. 314
  5. 315
  6. 316
  7. 317
  8. 318
  9. 319
  10. 320

Question 311

Multiple choice Writing Classes
Consider the Counter class below.
public class Counter
{
    private int value;
    private int step;

    public Counter(int start, int s)
    {
        value = start; step = s;
    }
    public void advance()
    {
        value = value + step;
        if (value > 10)
            value = value - 10;
    }
    public int getValue()
    {
        return value;
    }
}
What is returned by c.getValue() after executing:
Counter c = new Counter(7, 3);
for (int i = 0; i < 5; i++)
    c.advance();
Pick an answer, then check it. The solution opens in a window.

Question 312

Multiple choice Writing Classes
Consider the Box class.
public class Box
{
    private int width, height;

    public Box(int w, int h)
    {
        width = w;
        height = h;
    }

    public void scale(int factor)
    {
        width = width * factor;
        height = height * factor;
    }
    public String toString()
    {
        return width + " " + height;
    }
}
What is printed by the following code?
Box b = new Box(3, 4);
b.scale(2);
b.scale(2);
System.out.println(b);
Pick an answer, then check it. The solution opens in a window.

Question 313

Multiple choice Writing Classes
The Tank class models a liquid container with a fixed capacity. Overfilling spills the excess.
public class Tank
{
    private int capacity, contents;

    public Tank(int cap)
    {
        capacity = cap;
        contents = 0;
    }

    public int add(int amt)
    {
        int spill = 0;
        contents = contents + amt;
        if (contents > capacity)
        {
            spill = contents - capacity;
            contents = capacity;
        }
        return spill;
    }
    public int getContents()
    {
        return contents;
    }
}
What is printed?
Tank t = new Tank(50);
System.out.print(t.add(20) + " ");
System.out.print(t.getContents() + " ");
System.out.print(t.add(45) + " ");
System.out.print(t.getContents());
Pick an answer, then check it. The solution opens in a window.

Question 314

Multiple choice Writing Classes
Consider the Product class, which applies a 25% bulk discount for orders of 10 or more.
public class Product
{
    private String name;
    private double price;
    private int quantity;

    public Product(String n, double p, int q)
    {
        name = n; price = p; quantity = q;
    }
    public double totalValue()
    {
        double base = price * quantity;
        if (quantity >= 10)
            base = base * 0.75;
        return base;
    }
}
What is returned by p.totalValue() for Product p = new Product("Pen", 7.0, 10);?
Pick an answer, then check it. The solution opens in a window.

Question 315

Multiple choice Writing Classes
Consider the Tracker class.
public class Tracker
{
    private static int total = 0;
    private int value;

    public Tracker(int v)
    {
        value = v;
        total = total + v;
    }
    public static int getTotal()
    {
        return total;
    }
}
What is printed?
Tracker a = new Tracker(3);
Tracker b = new Tracker(5);
System.out.print(Tracker.getTotal() + " ");
Tracker c = new Tracker(7);
System.out.print(Tracker.getTotal());
Pick an answer, then check it. The solution opens in a window.

Question 316

Multiple choice Writing Classes
Consider the Point class.
public class Point
{
    private int x, y;

    public Point(int a, int b)
    {
        x = a;
        y = b;
    }

    public void shift(int dx, int dy)
    {
        x = x + dx;
        y = y + dy;
    }

    public String toString()
    {
        return x + "," + y;
    }
}
What is printed?
Point p = new Point(2, 3);
Point q = p;
q.shift(3, 3);
System.out.print(p + " " + q);
Pick an answer, then check it. The solution opens in a window.

Question 317

Multiple choice Writing Classes
Consider the Account class, where absorb merges another account's balance into this one and zeros out the other.
public class Account
{
    private double balance;

    public Account(double b)
    {
        balance = b;
    }

    public void absorb(Account other)
    {
        balance = balance + other.balance;
        other.balance = 0;
    }
    public double getBalance()
    {
        return balance;
    }
}
What is printed?
Account a = new Account(100.0);
Account b = new Account(50.0);
System.out.print(a.getBalance() + " ");
System.out.print(b.getBalance() + " ");
a.absorb(b);
System.out.print(a.getBalance() + " ");
System.out.print(b.getBalance());
Pick an answer, then check it. The solution opens in a window.

Question 318

Multiple choice Writing Classes
Consider the Rectangle class.
public class Rectangle
{
    private int w, h;

    public Rectangle(int a, int b)
    {
        w = a;
        h = b;
    }

    public boolean isSquare()
    {
        return w == h;
    }

    public boolean fitsWithin(Rectangle other)
    {
        return w <= other.w && h <= other.h;
    }
}
What is printed?
Rectangle r1 = new Rectangle(4, 4);
Rectangle r2 = new Rectangle(5, 6);
System.out.print(r1.isSquare() + " ");
System.out.print(r2.isSquare() + " ");
System.out.print(r1.fitsWithin(r2) + " ");
System.out.print(r2.fitsWithin(r1));
Pick an answer, then check it. The solution opens in a window.

Question 319

Multiple choice Writing Classes
Consider the Score class, which accumulates test scores. The 2026 Quick Reference allows the cast (double) only in this unit for illustration purposes — treat it as standard int→double promotion via division.
public class Score
{
    private int sum, count;

    public Score()
    {
        sum = 0;
        count = 0;
    }

    public void record(int pts)
    {
        sum = sum + pts;
        count++;
    }

    public double average()
    {
        if (count == 0)
            return 0.0;
        return 1.0 * sum / count;
    }
}
What is returned by s.average() after:
Score s = new Score();
s.record(80);
s.record(90);
s.record(85);
Pick an answer, then check it. The solution opens in a window.

Question 320

Multiple choice Writing Classes
Consider the Timer class.
public class Timer
{
    private int ticks;

    public Timer(int t)
    {
        ticks = t;
    }

    public void tick()
    {
        if (ticks > 0)
            ticks--;
    }
    public int getTicks()
    {
        return ticks;
    }
}
What is printed?
Timer t = new Timer(10);
for (int i = 0; i < 2; i++)
    t.tick();
System.out.print(t.getTicks() + " ");
for (int i = 0; i < 20; i++)
    t.tick();
System.out.print(t.getTicks() + " ");
t.tick();
System.out.print(t.getTicks());
Pick an answer, then check it. The solution opens in a window.
Question 311 of 402
Page 32 of 41