AP CSA Unit-Level MCQ
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.
Answered 0 of 15
Choose one answer.
Code Tracing 01 - Object State and Encapsulation: track the final printed value.
public class Main {
public static void main(String[] args) {
Counter c = new Counter(18);
c.bump(3);
c.bump(-3);
c.bump(3);
System.out.print(c.getValue());
}
}
class Counter {
private int value;
public Counter(int start) { value = start; }
public void bump(int amount) { value += amount; }
public int getValue() { return value; }
}All 15 object state and encapsulation questions
Work through the interactive quiz above first. This is the full object state and encapsulation question set with worked solutions, so students can review any question after attempting it.
1.Code Tracing 01 - Object State and Encapsulation: track the final printed value.
public class Main { public static void main(String[] args) { Counter c = new Counter(18); c.bump(3); c.bump(-3); c.bump(3); System.out.print(c.getValue()); } } class Counter { private int value; public Counter(int start) { value = start; } public void bump(int amount) { value += amount; } public int getValue() { return value; } }- 15
- 21
- 18
- 27
Show worked solution
Correct answer: 21
Each call mutates the same private field, so the three bumps accumulate onto one running value.
2.Code Tracing 12 - Object State and Encapsulation: follow the variable updates.
public class Main { public static void main(String[] args) { Box a = new Box(26); Box b = a; b.setValue(7); System.out.print(a.getValue() + ":" + b.getValue()); } } class Box { private int value; public Box(int v) { value = v; } public void setValue(int v) { value = v; } public int getValue() { return value; } }- 7:26
- 26:26
- 7:7
- 26:7
Show worked solution
Correct answer: 7:7
Both variables refer to one object, so a change made through either name is visible through the other.
3.Code Tracing 28 - Object State and Encapsulation: trace the branch and loop path.
public class Main { public static void main(String[] args) { Box a = new Box(38); Box b = new Box(38); System.out.print((a == b) + ":" + (a.getValue() == b.getValue())); } } class Box { private int value; public Box(int v) { value = v; } public int getValue() { return value; } }- false:false
- true:false
- false:true
- true:true
Show worked solution
Correct answer: false:true
Two separately constructed objects are never == to each other, even when the state they hold is identical.
4.Code Tracing 39 - Object State and Encapsulation: watch the index changes.
public class Main { public static void main(String[] args) { Account acct = new Account(46); acct.withdraw(99999); System.out.print(acct.getBalance()); } } class Account { private int balance; public Account(int b) { balance = b; } public void withdraw(int amt) { if (amt <= balance) balance -= amt; } public int getBalance() { return balance; } }- -99953
- 0
- -99999
- 46
Show worked solution
Correct answer: 46
The guard inside the mutator is what protects the private field, so an invalid request leaves the state untouched.
5.Code Tracing 50 - Object State and Encapsulation: evaluate the state change step by step.
public class Main { public static void main(String[] args) { Pair p = new Pair(54, 7); p.swap(); System.out.print(p.getFirst() + ":" + p.getSecond() + ":" + p.sum()); } } class Pair { private int first; private int second; public Pair(int f, int s) { first = f; second = s; } public void swap() { int t = first; first = second; second = t; } public int getFirst() { return first; } public int getSecond() { return second; } public int sum() { return first + second; } }- 7:54:61
- 54:7:61
- 7:54:0
- 54:54:61
Show worked solution
Correct answer: 7:54:61
Swapping reorders the internal fields but leaves any value derived from both of them unchanged.
6.What is printed by the following code segment?
static class A { private int n; public void add(int v) { if (v > 0) { n += v; } } public int get() { return n; } } public static void main(String[] args) { A a = new A(); a.add(5); a.add(-3); a.add(2); System.out.print(a.get()); }- 7
- 4
- 0
- 10
Show worked solution
Correct answer: 7
The guard silently ignores the negative argument, so only two additions take effect.
7.What is printed by the following code segment?
static class B { private int[] d; public B(int[] src) { d = src; } public int first() { return d[0]; } } public static void main(String[] args) { int[] x = {1, 2}; B b = new B(x); x[0] = 9; System.out.print(b.first()); }- 1
- 9
- 0
- 2
Show worked solution
Correct answer: 9
Storing the caller reference lets outside code keep changing the object state.
8.What is printed by the following code segment?
static class C { private int[] d; public C(int[] src) { d = new int[src.length]; for (int i = 0; i < src.length; i++) { d[i] = src[i]; } } public int first() { return d[0]; } } public static void main(String[] args) { int[] x = {1, 2}; C c = new C(x); x[0] = 9; System.out.print(c.first()); }- 9
- 0
- 1
- 2
Show worked solution
Correct answer: 1
A defensive copy insulates the object from later changes made by the caller.
9.What is printed by the following code segment?
static class D { private int n = 10; public void half() { n /= 2; } public int get() { return n; } } public static void main(String[] args) { D d = new D(); for (int i = 0; i < 4; i++) { d.half(); } System.out.print(d.get()); }- 1
- 2
- 5
- 0
Show worked solution
Correct answer: 0
Integer division reaches 5, 2, 1, then 0 across the four calls.
10.What is printed by the following code segment?
static class E { private boolean used = false; public boolean use() { if (used) { return false; } used = true; return true; } } public static void main(String[] args) { E e = new E(); System.out.print(e.use() + " " + e.use() + " " + e.use()); }- true false false
- true true true
- false false false
- true false true
Show worked solution
Correct answer: true false false
The first call flips the flag, so every later call is refused.
11.What is printed by the following code segment?
static class F { private int lo = 100; private int hi = 0; public void record(int v) { if (v < lo) { lo = v; } if (v > hi) { hi = v; } } public int range() { return hi - lo; } } public static void main(String[] args) { F f = new F(); f.record(30); f.record(70); System.out.print(f.range()); }- 100
- 40
- -100
- 70
Show worked solution
Correct answer: 40
Seeding the bounds at extremes lets the first two readings set both correctly.
12.What is printed by the following code segment?
static class G { private int c; public G bump() { c++; return this; } public int get() { return c; } } public static void main(String[] args) { G g = new G(); System.out.print(g.bump().bump().bump().get()); }- 1
- 0
- 3
- 2
Show worked solution
Correct answer: 3
Returning the current object allows the calls to be chained, and each one increments the same field.
13.What is printed by the following code segment?
static class H { private String s = ""; public void add(String t) { s += t; } public void reset() { s = ""; } public int len() { return s.length(); } } public static void main(String[] args) { H h = new H(); h.add("abc"); h.reset(); h.add("de"); System.out.print(h.len()); }- 5
- 3
- 0
- 2
Show worked solution
Correct answer: 2
The reset discards everything accumulated before it.
14.What is printed by the following code segment?
static class I2 { private int a = 1; public void set(int v) { a = v; } public int get() { return a; } } public static void main(String[] args) { I2 x = new I2(); I2 y = x; y.set(5); x = new I2(); System.out.print(x.get() + " " + y.get()); }- 1 5
- 5 5
- 1 1
- 5 1
Show worked solution
Correct answer: 1 5
Reassigning x points it at a fresh object while y still refers to the modified one.
15.What is printed by the following code segment?
static class J { private int n; public void set(int v) { n = v < 0 ? 0 : v; } public int get() { return n; } } public static void main(String[] args) { J j = new J(); j.set(-5); int a = j.get(); j.set(8); System.out.print(a + " " + j.get()); }- -5 8
- 0 8
- 0 0
- -5 -5
Show worked solution
Correct answer: 0 8
The mutator clamps a negative argument to zero before storing it.
