Diagnose a getter that changes the count

A step counter starts with steps equal to 6. Its programmer intended getSteps() to be an accessor, but the implementation contains an unintended update. Read both methods and the calling code, then select every true statement.


Java
public int getSteps() {
    steps = steps + 1;
    return steps;
}

public boolean setSteps(int proposed) {
    if (proposed < 0) {
        return false;
    }
    steps = proposed;
    return true;
}


Starting from the initial value of 6, the following statements run once:


Java
int first = counter.getSteps();
boolean accepted = counter.setSteps(first - 10);
int second = counter.getSteps();
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Accessors and Mutators Questions: