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.
Subtopic: Writing Classes Clear filter
Question 272
Question 273
Question 274
Question 275
Question 276
Question 277
Question 278
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();Main Solution
Solution:
Correct: B. Start value=7, step=3. i=0: 7+3=10 (not >10). i=1: 10+3=13 > 10 → 3. i=2: 3+3=6. i=3: 6+3=9. i=4: 9+3=12 > 10 → 2.
Answer: Correct: B
Answer images
Question 279
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);Main Solution
Solution:
Correct: D. Start (3, 4). First scale(2) → (6, 8). Second scale(2) → (12, 16). toString returns "12 16".
Answer: Correct: D
Answer images
Question 280
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());Main Solution
Solution:
Correct: A. add(20): contents=20, no overflow → returns 0. getContents=20. add(45): contents=65 > 50 → spill=15, contents clamped to 50 → returns 15. getContents=50. Output: 0 20 15 50.
Answer: Correct: A
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

