AP CSA Practice Test: Class Design
AP CSA Practice Test: Class Design
Unit 3: Class Creation | 10 Questions | AP Computer Science A
public class Account {
public double balance; // Line 1
private String owner; // Line 2
public double getBalance() { // Line 3
return balance;
}
}
Explanation: Line 1 declares balance as public, which violates encapsulation. Instance variables should be private with public accessor/mutator methods. This would lose points on the AP FRQ.
Common Mistake: Thinking public instance variables are acceptable. AP CSA always requires private instance variables.
public class Point {
private int x, y;
public Point(int x, int y) {
x = x;
y = y;
}
public String toString() {
return x + "," + y;
}
}
// In main:
Point p = new Point(3, 7);
System.out.println(p);
Explanation: Classic shadowing bug: x = x assigns the parameter to itself, NOT to the instance variable. The instance variables remain at their default value of 0. Fix: use this.x = x.
Common Mistake: Missing the shadowing issue. Without 'this', the parameter hides the instance variable.
private String name?public void getName()
public String getName()
private String getName()
public void setName(String n)
Explanation: An accessor (getter) must: be public, return the type of the variable (String), take no parameters, and NOT be void. Option D is a mutator (setter), not an accessor.
Common Mistake: Choosing void return type. Accessors must return a value; mutators are void.
public class Counter {
private int count;
public void increment() {
count++;
}
public void increment(int n) {
count += n;
}
public int getCount() {
return count;
}
}
// In main:
Counter c = new Counter();
c.increment();
c.increment(5);
c.increment();
System.out.println(c.getCount());
Explanation: The class has overloaded increment methods. increment() adds 1, increment(5) adds 5. Calls: +1, +5, +1 = 7. count defaults to 0.
Common Mistake: Forgetting that int instance variables default to 0, not undefined.
I. A constructor must have the same name as the class
II. A constructor can have a return type of void
III. A class can have multiple constructors with different parameter lists
Explanation: I: true, constructors always match the class name. II: false, constructors have NO return type (not even void). If you add void, it becomes a regular method. III: true, this is constructor overloading.
Common Mistake: Thinking void is a valid constructor return type. Constructors have no return type at all.
public class Dog {
private String name;
private int age;
public Dog(String name) {
this.name = name;
this.age = 1;
}
public Dog(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return name + " (" + age + ")";
}
}
// In main:
Dog d = new Dog("Rex");
System.out.println(d);
Explanation: The one-parameter constructor is called, which sets name to "Rex" and age to 1. The toString() method formats the output as "Rex (1)".
Common Mistake: Expecting age = 0. The one-parameter constructor explicitly sets age = 1.
public class Pair {
private int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public boolean equals(Object obj) {
Pair other = (Pair) obj;
return this.a == other.a && this.b == other.b;
}
}
// In main:
Pair p1 = new Pair(3, 5);
Pair p2 = new Pair(3, 5);
System.out.println(p1 == p2);
System.out.println(p1.equals(p2));
Explanation: == compares references (different objects = false). .equals() uses the overridden method which compares field values (3==3 and 5==5 = true).
Common Mistake: Thinking == compares content for objects. It always compares references.
public class Vehicle {
private String type;
public Vehicle(String type) {
this.type = type;
}
}
// In main:
Vehicle v = new Vehicle();
Explanation: When you define ANY constructor, Java no longer provides a default no-argument constructor. Since only Vehicle(String) exists, calling new Vehicle() with no arguments causes a compile error.
Common Mistake: Assuming Java always has a default constructor. It only exists if you define NO constructors.
Explanation: Encapsulation requires that private data is accessed only through the object's own public methods. Directly modifying another object's private variable violates this principle. (In practice, Java prevents this at compile time.)
Common Mistake: Confusing encapsulation with abstraction. Encapsulation specifically addresses data hiding.
toString() allow you to do?System.out.println
Explanation: When you pass an object to System.out.println(), Java automatically calls its toString() method. Overriding it lets you define a meaningful String representation instead of the default memory address.
Common Mistake: Confusing toString() with equals(). toString() is for display; equals() is for comparison.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]