AP Computer Science A Writing Classes — Worked Answer Explanations
Unit 5 · 12 questions explained
Below is a complete answer key for our AP Computer Science A Writing Classes practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.
Prefer to test yourself first? Take the timed Writing Classes practice test and come back here to review, or head back to the Writing Classes unit overview.
- Question 1 · Easy
Which of the following correctly defines a constructor for a
Dogclass?public class Dog { private String name; // Which option is a valid constructor? }- A
public void Dog(String n) { name = n; }Why not A: Constructors have no return type — not evenvoid. The presence ofvoidmakes this a regular method, not a constructor. - B
public Dog(String n) { name = n; }Correct - C
private Dog(String n) { name = n; }Why not C: A private constructor is syntactically valid but rarely used (prevents outside instantiation). The standard AP CSA convention usespublic. However, this IS a valid constructor — Option B is more clearly correct for the AP context. - D
public dog(String n) { name = n; }Why not D: The constructor name must exactly match the class name. The class isDog(capital D);dog(lowercase) does not match and would be treated as a regular method missing a return type.
ExplanationA constructor has the same name as the class (exact match, including case) and has NO return type (not even void). Option B correctly defines a
public Dogconstructor that assigns the parameter to the instance variable.Key takeawayA constructor has the same name as the class and no return type — not even void.
- A
- Question 2 · Easy
What is the output of the following code?
public class Counter { private int count = 0; public void increment() { count++; } public int getCount() { return count; } } // In main: Counter c = new Counter(); c.increment(); c.increment(); c.increment(); System.out.println(c.getCount());- A0Why not A: Each call to
increment()increasescountby 1. After 3 calls, count is 3, not 0. - B3Correct
- C1Why not C: Three separate
increment()calls are made. Each call increments the same instance variable. - DCompile error: count is private.Why not D: Private fields are accessible within the class.
increment()andgetCount()are methods in the same class, so they can accesscount.
ExplanationThree calls to
increment()each add 1 tocount, which starts at 0. After three increments,count = 3.getCount()returns the current value ofcount.Key takeawayInstance variables are shared across method calls on the same object. Each method call can modify the object's state.
- A
- Question 3 · Easy
Given the following class, which statement correctly accesses the
namevariable from OUTSIDE the class?public class Person { private String name; public Person(String n) { name = n; } public String getName() { return name; } }- A
Person p = new Person("Alice"); System.out.println(p.name);Why not A:nameis private. Accessing it directly from outside the class causes a compile error. - B
Person p = new Person("Alice"); System.out.println(p.getName());Correct - C
Person p = new Person("Alice"); System.out.println(Person.name);Why not C:nameis an instance variable (not static). You cannot access it through the class name. - D
Person p = new Person("Alice"); System.out.println(name);Why not D:nameis not in scope outside the Person class. You must call the getter method on an instance.
ExplanationPrivate fields can only be accessed directly within the class. From outside, you must use the public getter
getName(). This is encapsulation — hiding implementation details and providing controlled access.Key takeawayPrivate instance variables are not accessible outside the class. Use public getter methods to provide read access.
- A
- Question 4 · Easy
What is the output of the following code?
public class Box { private int value; public Box(int v) { value = v; } public void doubleValue() { value = value * 2; } public int getValue() { return value; } } // In main: Box b1 = new Box(5); Box b2 = b1; b2.doubleValue(); System.out.println(b1.getValue());- A5Why not A:
b2 = b1makes both variables refer to the SAME object. Callingb2.doubleValue()modifies the shared object, so b1.getValue() sees the change. - B10Correct
- C20Why not C:
doubleValue()is called only once. 5 * 2 = 10, not 20. - DCompile errorWhy not D: The code is valid. Assigning one object reference to another is legal in Java.
Explanationb2 = b1copies the reference, not the object. Bothb1andb2point to the sameBoxobject in memory. Whenb2.doubleValue()is called, the shared object'svaluechanges from 5 to 10.b1.getValue()returns 10.Key takeawayAssigning an object reference copies the address, not the object. Both variables then point to the same object.
- A
- Question 5 · Easy
What is the output of the following code?
public class MathHelper { private int x; public MathHelper(int x) { this.x = x; } public int triple() { return x * 3; } public static int square(int n) { return n * n; } } // In main: MathHelper m = new MathHelper(4); System.out.println(m.triple()); System.out.println(MathHelper.square(5));- A12\n25Correct
- B12\n20Why not B:
square(5)returns 55=25, not 54=20. The static method receives 5 as its argument. - C16\n25Why not C:
triple()returns x3 = 43 = 12, not 4*4=16 (that would be square). - DCompile error: cannot call static method on instance.Why not D:
MathHelper.square(5)is the correct way to call a static method — via the class name. This is valid Java.
Explanationm.triple()uses the instance variable x=4 and returns 43=12.MathHelper.square(5)is a static method called on the class (not an instance) and returns 55=25.this.x = xin the constructor usesthisto distinguish the instance variable from the parameter.Key takeawayStatic methods belong to the class and are called via the class name. Instance methods belong to an object and are called via a reference.
- A
- Question 6 · Easy
Which of the following best explains the purpose of the
thiskeyword in the following constructor?public class Point { private double x; private double y; public Point(double x, double y) { this.x = x; this.y = y; } }- A
thiscalls the parent class constructor.Why not A:super()(notthis) calls the parent class constructor.thisrefers to the current object. - B
this.xrefers to the instance variablex, distinguishing it from the parameterx.Correct - C
thisis required whenever assigning values in a constructor.Why not C:thisis only needed when a parameter name shadows an instance variable. If they had different names (e.g., parameterxVal),thiswould be optional. - D
this.x = xassigns the instance variable to the parameter.Why not D: The direction is reversed:this.x = xassigns the parameter value (xon the right) TO the instance variable (this.xon the left).
ExplanationWhen a constructor parameter has the same name as an instance variable,
this.xrefers to the instance variable whilexalone refers to the parameter.this.x = xassigns the parameter's value to the instance variable.Key takeaway`this` refers to the current object. Use `this.field` to distinguish instance variables from parameters with the same name.
- A
- Question 7 · Easy
What is the output of the following code?
public class Student { private String name; private int grade; public Student(String name, int grade) { this.name = name; this.grade = grade; } public void setGrade(int grade) { if (grade >= 0 && grade <= 100) { this.grade = grade; } } public int getGrade() { return grade; } } // In main: Student s = new Student("Ali", 85); s.setGrade(110); System.out.println(s.getGrade());- A110Why not A: The setter validates the input. 110 is outside 0–100, so the assignment is skipped and grade stays at 85.
- B85Correct
- C0Why not C: The setter only ignores invalid input; it does not reset the grade to 0. The grade retains its previous value of 85.
- DCompile errorWhy not D: This is valid Java. The setter is a standard validation pattern where invalid inputs are ignored.
ExplanationThe
setGrademethod validates the new grade: 110 is NOT in the range [0, 100], so the body of the if statement does NOT execute. The instance variablegraderemains unchanged at 85.Key takeawaySetters can validate input before modifying instance variables — a key benefit of encapsulation.
- A
- Question 8 · Easy
What is the output of the following code?
public class Accumulator { private static int total = 0; private int value; public Accumulator(int v) { value = v; total += v; } public static int getTotal() { return total; } } // In main: Accumulator a1 = new Accumulator(10); Accumulator a2 = new Accumulator(20); Accumulator a3 = new Accumulator(5); System.out.println(Accumulator.getTotal());- A5Why not A:
totalis static and shared among all instances. Each constructor call adds to the sametotalvariable. 10+20+5=35. - B35Correct
- C10Why not C: Only the last constructor's value would give 5, and only the first would give 10.
totalaccumulates all three. - D0Why not D:
totalstarts at 0 but is modified by each constructor call. After creating a1, a2, a3, total = 10+20+5 = 35.
Explanationtotalis astaticvariable shared by all instances of the class. Each constructor call adds tototal: 0+10=10, 10+20=30, 30+5=35.getTotal()returns 35.Key takeawayStatic variables are shared across all instances. Each object creation that modifies a static variable affects all instances.
- A
- Question 9 · Easy
What is the output of the following code?
public class Thing { private int x; public Thing() { x = 10; } public Thing(int x) { this.x = x; } public int getX() { return x; } } // In main: Thing a = new Thing(); Thing b = new Thing(7); System.out.println(a.getX() + b.getX());- A17Correct
- B7Why not B:
auses the no-arg constructor, setting x=10. 10 + 7 = 17, not just 7. - C10Why not C: Both objects are created. a.getX() = 10, b.getX() = 7, and 10+7 = 17.
- DCompile error: duplicate constructor definitions.Why not D: Java allows constructor overloading — multiple constructors with different parameter lists. This is valid.
ExplanationConstructor overloading allows multiple constructors with different signatures.
new Thing()calls the no-arg constructor, setting x=10.new Thing(7)calls the one-arg constructor, setting x=7. Sum = 10+7 = 17.Key takeawayA class can have multiple constructors (overloading) as long as their parameter lists differ in type or number.
- A
- Question 10 · Medium
Given the following class definition, which of the following correctly modifies the
balancefrom a test classBankTester?public class BankAccount { private double balance; public BankAccount(double initial) { balance = initial; } public void deposit(double amount) { if (amount > 0) balance += amount; } public double getBalance() { return balance; } }- A
BankAccount acct = new BankAccount(100); acct.balance = 200;Why not A:balanceis private. Direct field access from outside the class causes a compile error. - B
BankAccount acct = new BankAccount(100); acct.deposit(50);Correct - C
BankAccount.balance = 200;Why not C:balanceis an instance variable, not static. It cannot be accessed via the class name. - D
BankAccount acct = new BankAccount(100); acct.deposit(-50);Why not D: Thedepositmethod validates thatamount > 0. A negative deposit (-50) is ignored; balance stays at 100.
ExplanationOption B correctly calls the public
depositmethod with a positive amount. Private fields must be modified through public methods. Option A fails becausebalanceis private. Option D fails because -50 does not satisfyamount > 0.Key takeawayEncapsulation: private fields are modified only through public methods. This allows the class to enforce valid state.
- A
- Question 11 · Medium
What is the output of the following code?
public class Counter { private int count; public Counter(int start) { count = start; } public Counter add(int n) { count += n; return this; } public int getCount() { return count; } } // In main: Counter c = new Counter(0); System.out.println(c.add(3).add(4).getCount());- A3Why not A:
add(3)returnsthis(the same Counter), thenadd(4)adds 4 more to the same object. Total = 3+4 = 7. - B7Correct
- CCompile error:
addreturns Counter, not void.Why not C: Returningthisfrom a method enables method chaining. The code is valid and commonly used in fluent-style APIs. - D4Why not D: Method chaining:
c.add(3)modifies count to 3 and returns the same Counter..add(4)then adds 4 to get 7.
Explanationadd(3)adds 3 to count (now 3) and returnsthis(the same Counter object)..add(4)adds 4 to count (now 7) and returnsthis..getCount()returns 7. Returningthisenables fluent method chaining.Key takeawayReturning `this` from a method enables method chaining — each call operates on the same object.
- A
- Question 12 · Medium
What is printed by the following code?
public class Temp { private double celsius; public Temp(double c) { celsius = c; } public double toFahrenheit() { return celsius * 9 / 5 + 32; } public String toString() { return celsius + "C"; } } // In main: Temp t = new Temp(100); System.out.println(t); System.out.println(t.toFahrenheit());- ATemp@[hashcode]\n212.0Why not A: The class overrides
toString(), soprintln(t)calls the overridden version and prints "100.0C", not the default hash. - B100.0C\n212.0Correct
- C100C\n212.0Why not C: The constructor takes a double (100 is widened to 100.0).
toString()returnscelsius + "C"= "100.0C" (double concatenated with String). - D100.0C\n180.0Why not D: The formula is 100*9/5+32 = 900/5+32 = 180+32 = 212. The +32 is part of the Fahrenheit conversion.
ExplanationOverriding
toString()gives objects a custom String representation.println(t)implicitly callst.toString()which returns "100.0C" (celsius is stored as 100.0).toFahrenheit()= 100*9/5+32 = 180+32 = 212.0.Key takeawayOverriding `toString()` customizes how an object is printed. `println(obj)` calls `toString()` automatically.
- A