Class creation practice test
Up to 15 AP Computer Science A questions on class creation, timed at the exam's pace and scored, new every time. Try 3 below first.
Class creation is 10%–18% of the AP Computer Science A exam's multiple choice. Learn it first in the class creation chapter.
3 free class creation practice questions
Real questions from the Atlas AP Computer Science A exam pool, with the answer and the working.
Consider the following class.
public class Cell { private int val; public Cell(int v) { val = v; } public void absorb(Cell other) { val += other.val; other.val = 0; } public int getVal() { return val; } }Consider the following code segment.
Cell a = new Cell(4); Cell b = new Cell(7); Cell c = b; a.absorb(b); c.absorb(a); System.out.println(a.getVal() + " " + b.getVal() + " " + c.getVal());What is printed as a result of executing the code segment?
- A0 0 18
- B0 11 11
- C11 18 18
- D0 0 0
Show the answer
B.
c refers to the same object as b.
a.absorb(b)makes a 11 and b 0.c.absorb(a)is b absorbing a: b becomes 11 and a 0. b and c are one object, so both print 11.0 0 18: treatsc = bas making a copy: it makes c refer to the same Cell object.11 18 18: forgetsother.val = 0: a method of Cell can change another Cell's private variable.0 0 0: zeroesother.valbefore adding it.Consider the following class.
public class Scorebook { private int total; private int count; public void add(int score) { total += score; count++; } /** Returns the mean of the scores added so far, * or 0.0 if none have been added */ public double average() { /* missing code */ } }Which of the following can replace
/* missing code */so thataverageworks as intended?- A
return total / count; - B
if (count == 0) { return 0.0; } return total / count; - C
return (double) (total / count); - D
if (count == 0) { return 0.0; } return (double) total / count;
Show the answer
D.
The check for count == 0 avoids dividing by zero, and casting total to double before dividing keeps the fraction: scores 7 and 8 give 7.5.
return total / count;does int division (7.0 instead of 7.5) and throws an ArithmeticException when count is 0.if (count == 0) { return 0.0; } return total / count;handles 0 but still truncates.(double) (total / count)casts after the int division has dropped the fraction, and also fails when count is 0.- A
Consider the following class.
public class Counter { private int count; private static int made = 0; public Counter() { count = 0; made++; } public static int getMade() { return made + count; } }Which of the following best describes the class?
- AIt does not compile, because the static method
getMadeuses the instance variablecount. - BIt compiles, and
getMade()returns the number of Counter objects made, since count is always 0. - CIt does not compile, because the private variable
madecannot be changed in the constructor. - DIt compiles, but
madeis reset to 0 each time a Counter is constructed.
Show the answer
A.
A static method belongs to the class, not to any object, so it has no
countto use: referring to an instance variable in a static method is a compile-time error.The value count happens to hold does not matter, because the code never compiles. Constructors can change private variables, static ones included. A static variable is initialized once, when the class is loaded, not each time an object is made.
- AIt does not compile, because the static method
Questions people ask
- What is in the AP Computer Science A Class creation practice test?
- Up to 15 multiple-choice questions from this unit, plus a free-response question when the unit has one, timed at the real exam's pace and scored as a percent. The questions are new every time, drawn from a pool of checked questions.
- How much of the AP Computer Science A exam is Class creation?
- 10% to 18% of the multiple-choice questions, by the College Board’s course and exam description.
- Is it free?
- The 3 questions on this page need no account. With a free Atlas account your first unit test is free; Atlas Premium has no limit.