AP CSA Unit 3 Day 11: Mutator Method

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?
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
View Unit 3 Study Guide

Ready for More Challenges?

Cycle 2 questions prepare you for the hardest AP CSA exam questions.

Study Games Practice FRQs
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.