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());- A. 101
- B. Compilation error: interfaces cannot declare private methods
- C. 100
- 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
- What is printed? cpp class Account { double balance = 0; friend void deposit(Account& a, double amt); public: double getBalance() const {…
- What is printed? java abstract class Shape { abstract double area(); } interface Colorable { String color(); } class Circle extends Shape…
- What is printed? java class Base implements Cloneable { int x = 5; @Override public Base clone() throws CloneNotSupportedException {…
- What is printed? java class Node<T extends Comparable<T>> { T value; Node(T v) { value = v; } boolean isGreaterThan(Node<T> other) {…
- What is printed? cpp struct Base { virtual void greet() const { cout << "Base "; } }; struct Derived : Base { void greet() const override…
- What is printed? cpp class Base { public: Base(const Base&) = delete; Base() = default; virtual void show() { cout << "Base"; } }; class…