AP CSA Unit 3 Day 7: Scope And Access

Unit 3, Classes & Objects • Cycle 2
Day 7 Advanced Practice • Harder Difficulty
Focus: Scope and Access Hard Scope and Access

Advanced Practice Question

Format: Scope and Access

Which line will cause a compilation error?
public class Widget {
    private int val = 5;
    
    public void methodA() {
        int val = 10;          // Line 1
        System.out.println(val);    // Line 2
        System.out.println(this.val); // Line 3
    }
}
Difficulty: Hard  |  Topic: Scope and Access  |  Cycle: 2 (Advanced)
Why This Answer?

Local variables can have the same name as instance variables. Line 2 prints the local val (10), Line 3 prints the instance val (5). All lines are valid.

Common Mistake
Watch Out!

Thinking you can't shadow instance variables with local variables.

AP Exam Strategy

Local variables shadow instance variables; use this. to access the instance version.

Master This Topic

This Cycle 2 HARD question tests scope and access. Review Unit 3 concepts to build mastery of classes & objects.

  • Understanding scope and access
  • 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.