Accessor Methods

AP Computer Science A· difficulty 3/5

public class Wrap {
    private StringBuilder b = new StringBuilder("hi");
    public StringBuilder getB() { return b; }
}
// ...
Wrap w = new Wrap();
w.getB().append("!");
System.out.println(w.getB());

What is printed?

  • A

    hi!

    check_circle
  • B

    !

  • C

    hi

  • D

    Compile-time error

Explanation

The getter returns the same mutable reference; appending modifies the underlying StringBuilder.

Want 10 more like this — adaptive to your weak spots?

Related questions