Unit 3 Practice Test: Class Creation
Practice AP Computer Science A Unit 3 with tests on writing Java classes, constructors, encapsulation, accessor and mutator methods, and static members.
Class Creation in AP Computer Science A
Unit 3 moves from using pre-built classes to designing your own. Writing a complete, well-structured Java class is the most heavily weighted FRQ skill on the AP exam. Mastery of Unit 3 concepts is directly tied to performance on the class design free-response question.
Writing a Java Class
A Java class defines the blueprint for objects. It specifies what data each object holds and what behaviors it can perform. The AP exam frequently asks you to write an entire class from a written description, requiring you to translate requirements into correct Java syntax and structure.
Constructors
A constructor initializes a new object's instance variables. AP FRQ tasks often require writing both a no-argument constructor and a parameterized constructor. A common mistake is writing a constructor with the wrong parameter types or failing to assign parameters to instance variables using this.
Instance Variables and Access Modifiers
Instance variables store the state of an object. In AP CSA, instance variables are declared private to enforce encapsulation. Declaring them public is a common error that earns no credit on FRQ scoring rubrics.
Accessor and Mutator Methods
Accessor methods (getters) return the value of a private instance variable. Mutator methods (setters) allow controlled modification. AP FRQs routinely ask you to write both types, and the graders check for correct return types, correct parameter lists, and proper use of this or direct variable references.
Static Methods and Variables
Static members belong to the class rather than to any individual object. Static variables maintain shared state across all instances. Static methods — like those in the Math class — are called on the class name, not on an object. AP MCQ questions test whether students can distinguish static from instance context.
Common Mistakes in Unit 3
- Missing constructors: Forgetting to write a constructor means objects cannot be properly initialized.
- Incorrect access modifiers: Declaring instance variables as public instead of private violates encapsulation and loses FRQ points.
- Encapsulation errors: Returning a direct reference to a mutable object from an accessor method instead of a copy.
- Constructor vs. method confusion: Writing a constructor with a return type — a constructor has no return type, not even void.
FRQ Strategy for Class Design Questions
When tackling a class design FRQ, read the prompt carefully to identify all required instance variables, then write the constructor(s), then write each method. Check that every private variable is initialized in the constructor and that accessor and mutator methods use correct return and parameter types.