Skip to main content

Unit 3 · Class Creation

Object State and Encapsulation

Practice mode with 15 Java-focused questions, immediate answer checks, and explanations.

What object state and encapsulation covers

Encapsulation means instance variables are private and outside code interacts through public methods. Accessors return state, mutators change it, and putting validation inside a mutator is what keeps an object from entering an impossible state. Two variables can refer to the same object, so a mutator called through either name changes what both observe; passing an object to a method works the same way.

Where students lose points

Students make instance variables public to make code compile, which the FRQ rubric penalizes. They also confuse accessors and mutators, returning a value from a setter or printing from a getter, and they miss aliasing questions by treating two references to one object as two independent objects.

How it shows up on the AP exam

Unit 3 content that is explicitly graded on class-design FRQs. Aliasing shows up separately on the MCQ as trace questions where a change through one reference appears through another.

For practice use only.

Object State and Encapsulation MCQ Practice

AP CSA encapsulation practice: private instance variables, public accessors and mutators, why state stays valid, and object reference aliasing.

Question 1 of 15

Answered 0 of 15

Choose one answer.

Which option matches the complete output produced by this segment?

public class Acc
{
    private int total;
    public void add(int v)
    {
        total += v;
    }
    public int getTotal()
    {
        return total;
    }
}

Acc a = new Acc();
a.add(3);
a.add(4);
System.out.print(a.getTotal());
Choose one answer.