ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE
A lambda expression captures a local variable from its enclosing method. What restriction does Java place on that variable?
- A. It must be declared volatile
- B. It must have a primitive type
- C. It must be declared static
- D. It must be effectively final: never reassigned anywhere after its initial assignment
Correct answer: D. It must be effectively final: never reassigned anywhere after its initial assignment
Java only allows a lambda to capture local variables that are final or effectively final, since the lambda may outlive the stack frame the variable lives in.
More OOP with C++ and Java questions
- In Java, instance method calls (non-static, non-private, non-final) use dynamic dispatch based on the object's actual run-time class…
- What is printed? cpp struct Vec2 { int x, y; Vec2 operator+(const Vec2& o) const { return {x + o.x, y + o.y}; } }; int main() { Vec2 a{1…
- A base class has a non-virtual destructor. A derived class owns a dynamically allocated member that it frees in its own destructor. What…
- java interface Greet { default String hello() { return "Hi"; } } class English implements Greet {} class French implements Greet { public…
- Why is operator<< for printing a user-defined type usually written as a free function (often declared a friend), rather than as a member…
- java class R implements AutoCloseable { public void close() { System.out.print("close"); } } try (R r = new R()) {…