AP CSA Unit 3 Day 6: This Keyword
Share
Unit 3, Classes & Objects • Cycle 2
Day 6 Advanced Practice • Harder Difficulty
Focus: this Keyword
Hard
this Keyword
Advanced Practice Question
Format: this Keyword
What is the output?
What is the output?
public class Point {
private int x;
public Point(int x) {
x = x;
}
public int getX() {
return x;
}
}
// In another class:
Point p = new Point(7);
System.out.println(p.getX());
Difficulty: Hard |
Topic: this Keyword |
Cycle: 2 (Advanced)
Why This Answer?
Without this.x, the statement `x = x` assigns the parameter to itself, leaving the instance variable x at its default value of 0.
Common Mistake
Watch Out!
Thinking x = x will somehow figure out which x is which.
AP Exam Strategy
Use this.x to refer to the instance variable when parameter names match.
Master This Topic
This Cycle 2 HARD question tests this keyword. Review Unit 3 concepts to build mastery of classes & objects.
- Understanding this keyword
- 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