ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
interface Counter {
    int next();
    private int helper() { return 100; }
    default int nextPlusHelper() { return next() + helper(); }
}
// in main:
int[] state = {0};
Counter c = () -> ++state[0];
System.out.println(c.nextPlusHelper());
  1. A. 101
  2. B. Compilation error: interfaces cannot declare private methods
  3. C. 100
  4. D. 1

Correct answer: A. 101

Private interface methods (Java 9+) are legal and can be called from default methods to share code without exposing it. The lambda implements next(), incrementing state[0] to 1 and returning 1; nextPlusHelper() adds helper()'s 100, giving 101.

More OOP with C++ and Java questions

Exams that ask this topic