AP CSA Unit 3 Day 5: Constructor Chaining
Share
Unit 3, Classes & Objects • Cycle 2
Day 5 Advanced Practice • Harder Difficulty
Focus: Constructor Chaining
Hard
Constructor Chaining
Advanced Practice Question
Format: Constructor Chaining
What is the output?
What is the output?
public class Thing {
private int x;
public Thing() {
this(5);
x = 10;
}
public Thing(int n) {
x = n;
}
public int getX() {
return x;
}
}
// In another class:
Thing t = new Thing();
System.out.println(t.getX());
Difficulty: Hard |
Topic: Constructor Chaining |
Cycle: 2 (Advanced)
Why This Answer?
The no-arg constructor calls this(5), setting x to 5. Then the no-arg constructor continues and sets x to 10. Final value is 10.
Common Mistake
Watch Out!
Thinking this(5) prevents the rest of the constructor from executing.
AP Exam Strategy
After this(...) completes, the current constructor continues executing.
Master This Topic
This Cycle 2 HARD question tests constructor chaining. Review Unit 3 concepts to build mastery of classes & objects.
- Understanding constructor chaining
- Tracing code execution accurately
- Avoiding common pitfalls
Ready for More Challenges?
Cycle 2 questions prepare you for the hardest AP CSA exam questions.
Study Games Practice FRQs