AP CSA Unit 3.3: Class Anatomy - Constructors and Methods Practice
Share
Unit 3, Section 3.3
Day 3 Practice • January 9, 2026
🎯 Focus: Anatomy of a Class
Practice Question
Consider the following class definition and code segment:
public class Rectangle {
private int width;
private int height;
public Rectangle(int w, int h) {
width = w;
height = h;
}
public int getArea() {
return width * height;
}
}
// Client code:
Rectangle r = new Rectangle(5, 3);
System.out.println(r.getArea());
What is printed as a result of executing the client code?
What This Tests: Section 3.3 covers the anatomy of a class—instance variables, constructors, and methods. This question tests how constructors initialize object state and how methods use that state.
Anatomy of a Class
public class Rectangle {
// 1. INSTANCE VARIABLES (state)
private int width;
private int height;
// 2. CONSTRUCTOR (initializes state)
public Rectangle(int w, int h) {
width = w; // Parameter → instance variable
height = h;
}
// 3. METHODS (behavior)
public int getArea() {
return width * height;
}
}
Step-by-Step Trace
| Step | Action | width | height |
|---|---|---|---|
| 1 | new Rectangle(5, 3) called | - | - |
| 2 | Constructor: w=5, h=3 | - | - |
| 3 | width = w | 5 | - |
| 4 | height = h | 5 | 3 |
| 5 | getArea(): 5 * 3 | 5 | 3 |
Output: 15 (5 × 3 = 15)
Common Mistakes
Mistake: Answer A (8)
This calculates perimeter (5+3 = 8) or misreads the dimensions. Area is width × height = 5 × 3 = 15.
Mistake: Answer C (0)
This assumes instance variables stay at default values. But the constructor explicitly sets width=5 and height=3.
Mistake: Answer D (53)
This concatenates 5 and 3 as strings. But width and height are ints, so * performs multiplication, not concatenation.
Constructor Key Points
Constructor Rules
- Same name as the class
- No return type (not even void)
- Called automatically with
new - Initializes instance variables
- Parameters provide initial values
Difficulty: Medium • Time: 2-3 minutes • AP Skill: 3.A - Instantiate objects
Ready to Level Up Your AP CSA Skills?
Get personalized help or access our complete question bank
Premium Question Bank - Coming Soon! Schedule 1-on-1 Tutoring