AP CSA Unit 3.4: Constructor Overloading Practice
Share
Unit 3, Section 3.4
Day 4 Practice • January 10, 2026
🎯 Focus: Constructor Overloading
Practice Question
Consider the following class definition and code segment:
public class Student {
private String name;
private int grade;
public Student() {
name = "Unknown";
grade = 0;
}
public Student(String n) {
name = n;
grade = 0;
}
public Student(String n, int g) {
name = n;
grade = g;
}
public String toString() {
return name + ": " + grade;
}
}
// Client code:
Student s = new Student("Alice");
System.out.println(s);
What is printed as a result of executing the client code?
What This Tests: Section 3.4 covers constructor overloading—having multiple constructors with different parameter lists. Java selects the constructor based on the number and types of arguments.
Constructor Selection
// Three overloaded constructors:
Student() // 0 parameters
Student(String n) // 1 parameter (String)
Student(String n, int g) // 2 parameters
// new Student("Alice") has 1 String argument
// → Matches Student(String n)
Step-by-Step Trace
| Step | Action | name | grade |
|---|---|---|---|
| 1 | new Student("Alice") called | - | - |
| 2 | Matches Student(String n) | - | - |
| 3 | name = n ("Alice") | "Alice" | - |
| 4 | grade = 0 | "Alice" | 0 |
| 5 | toString(): name + ": " + grade | "Alice: 0" | |
Common Mistakes
Mistake: Answer A (Unknown: 0)
This would be correct if we called new Student() with no arguments. But we passed "Alice", so the one-parameter constructor runs.
Mistake: Answer E (Student@hashcode)
This would print if there were no toString() method. But we defined toString(), so it returns "Alice: 0".
Overloading Rules
How Java Matches Constructors
Java matches based on the signature:
- Number of parameters
- Types of parameters (in order)
- NOT the parameter names
- NOT the return type (constructors have none)
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