ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
class Resource {
String name;
Resource(String n) { name = n; }
@Override
protected void finalize() { System.out.print("finalizing:" + name + " "); }
}Which statement about overriding finalize() here is correct, in modern Java (9+)?- A. It compiles and runs exactly once per garbage collection cycle, synchronously
- B. It compiles but is strongly discouraged and deprecated: finalize() timing is unpredictable and it may never run before the JVM exits
- C. It compiles and is guaranteed to run immediately when the object goes out of scope
- D. It fails to compile: finalize() cannot be overridden
Correct answer: B. It compiles but is strongly discouraged and deprecated: finalize() timing is unpredictable and it may never run before the JVM exits
Overriding Object.finalize() is legal syntax, but it has been deprecated since Java 9 specifically because its execution is neither guaranteed nor timely — the garbage collector may run it late, multiple times in edge cases historically, or not at all before JVM shutdown; try-with-resources/AutoCloseable is the recommended replacement.
More OOP with C++ and Java questions
- What is printed? cpp class Logger { public: static int instances; Logger() { instances++; } ~Logger() { instances--; } }; int…
- What is printed? cpp struct Event { virtual void handle() const { cout << "generic-event "; } }; struct ClickEvent : Event { void handle()…
- A lambda expression captures a local variable from its enclosing method. What restriction does Java place on that variable?
- 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…