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 281–290 of 359
  1. 281
  2. 282
  3. 283
  4. 284
  5. 285
  6. 286
  7. 287
  8. 288
  9. 289
  10. 290

Question 281

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 282

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 283

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 284

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 285

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 286

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 287

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 288

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

    public Point()
    {
        this(0, 0);
    }

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int sum()
    {
        return x + y;
    }
}
Consider the following code segment.
Point p = new Point();
Point q = new Point(3, 5);
System.out.println(p.sum() + q.sum());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 289

Multiple choice Writing Classes
Consider the following class.
public class Counter
{
    private static int count = 0;
    private int id;

    public Counter()
    {
        count++;
        id = count;
    }

    public int getId()
    {
        return id;
    }

    public static int getCount()
    {
        return count;
    }
}
Consider the following code segment.
Counter a = new Counter();
Counter b = new Counter();
Counter c = new Counter();
System.out.println(a.getId() + b.getId() + c.getId() + Counter.getCount());
What is printed when the code segment executes?
Pick an answer, then check it. The solution opens in a window.

Question 290

Multiple choice Writing Classes
Which of the following is true about a field declared private in a Java class, based on the rules of the AP Java subset?
Pick an answer, then check it. The solution opens in a window.
Question 281 of 359
Page 29 of 36