AP CSA Unit 3 Day 11: Mutator Method
Share
Unit 3, Classes & Objects • Cycle 2
Day 11 Advanced Practice • Harder Difficulty
Focus: Mutator Method
Hard
Mutator Method
Advanced Practice Question
Format: Mutator Method
What is the value of x after this code?
What is the value of x after this code?
public class Data {
private int x = 5;
public void setX(int x) {
x = x + 10;
}
}
// In another class:
Data d = new Data();
d.setX(3);
System.out.println(d.getX()); // assume getX() returns x
Difficulty: Hard |
Topic: Mutator Method |
Cycle: 2 (Advanced)
Why This Answer?
The mutator doesn't use this.x, so it just modifies the parameter x locally. The instance variable x remains 5.
Common Mistake
Watch Out!
Forgetting this.x in mutators when parameter names match instance variables.
AP Exam Strategy
In setters, ALWAYS use this.instanceVar when parameter names match.
Master This Topic
This Cycle 2 HARD question tests mutator method. Review Unit 3 concepts to build mastery of classes & objects.
- Understanding mutator method
- 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