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 271–280 of 359
  1. 271
  2. 272
  3. 273
  4. 274
  5. 275
  6. 276
  7. 277
  8. 278
  9. 279
  10. 280

Question 271

Multiple choice Writing Classes
Consider the following class. public class Counter { private int value; public void add(int n) { value = value + n; } public void increase() { this.add(3); }…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 272

Multiple choice Writing Classes
Consider the following code: public class Student { private String name; private int grade; public Student(String name, int grade) { name = name; grade = grade;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 273

Multiple choice Writing Classes
public class Counter { private int count; private int limit; public Counter(int limit) { this.limit = limit; count = 0; } public void increment() { if (count <…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 274

Multiple choice Writing Classes
Consider the following code: public class BankAccount { private double balance; private int transactionCount; public BankAccount(double initial) { balance =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 275

Multiple choice Writing Classes
Consider the following code: public class Temperature { private double celsius; public Temperature(double celsius) { this.celsius = celsius; } public double…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 276

Multiple choice Writing Classes
Consider the following code: public class Tracker { private int value; private int maxSoFar; public Tracker(int initial) { value = initial; maxSoFar = initial;…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 277

Multiple choice Writing Classes
Consider the following code: public class WordPair { private String first; private String second; public WordPair(String first, String second) { this.first =…

Sign up now. It's free!

See the full question with its answer and worked solution.

Question 278

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 279

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 280

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 271 of 359
Page 28 of 36