Can two invocations become one?

A Java program records how often an adjustment rule is invoked. Every fresh CountingOffset object starts with a stored call count of zero.


Java
interface IntRule {
    int apply(int value);
}

class CountingOffset implements IntRule {
    private int calls = 0;

    public int apply(int value) {
        calls += 1;
        return value + calls;
    }
}

class Processor {
    static int twice(IntRule rule, int value) {
        return rule.apply(value) + rule.apply(value);
    }
}


Consider each statement independently, starting with fresh objects as specified. Which statements are true? Select all that apply.

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 Function Objects Questions: