AP CSA Practice Test: Class Design

AP CSA Practice Test: Class Design

Unit 3: Class Creation | 10 Questions | AP Computer Science A

Question 1
Consider the following class. Which line has an encapsulation violation?
public class Account {
    public double balance;      // Line 1
    private String owner;       // Line 2
    
    public double getBalance() { // Line 3
        return balance;
    }
}
ALine 1
BLine 2
CLine 3
DNo violation exists

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.

Question 2
What is the output?
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);
A"3,7"
B"0,0"
CCompile error
DNullPointerException

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.

Question 3
Which method signature is a valid accessor method for a private instance variable private String name?
Apublic void getName()
Bpublic String getName()
Cprivate String getName()
Dpublic 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.

Question 4
Consider the following. What is printed?
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());
A5
B6
C7
DCompile error

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.

Question 5
Which of the following is true about constructors?

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
AI and II only
BI and III only
CII and III only
DI, II, and III

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.

Question 6
What is the output?
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);
A"Rex (0)"
B"Rex (1)"
C"null (0)"
DCompile error

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.

Question 7
Consider the following code. What is the result?
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));
Atrue then true
Bfalse then true
Ctrue then false
Dfalse then false

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.

Question 8
What happens when this code compiles?
public class Vehicle {
    private String type;
    public Vehicle(String type) {
        this.type = type;
    }
}
// In main:
Vehicle v = new Vehicle();
ACompiles and runs fine
BCompile error: no default constructor
CRuntime error
Dv.type is null

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.

Question 9
Which principle is violated when a method directly modifies another object's private instance variable?
APolymorphism
BEncapsulation
CInheritance
DAbstraction

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.

Question 10
What does toString() allow you to do?
AConvert an object to an int
BPrint an object's meaningful representation with System.out.println
CCompare two objects for equality
DCreate a copy of an object

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.

0% 0/10

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]