Mutator Methods

AP Computer Science A· difficulty 2/5

public class Box {
    private int size;
    public Box() { size = 1; }
    public void setSize(int s) { size = s; }
    public int getSize() { return size; }
}
// ...
Box b = new Box();
b.setSize(10);
System.out.println(b.getSize());

What is printed?

  • A

    1

  • B

    0

  • C

    Compile-time error

  • D

    10

    check_circle

Explanation

The mutator setSize assigns 10 to size, which getSize then returns.

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

Related questions