ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
In Java, instance method calls (non-static, non-private, non-final) use dynamic dispatch based on the object's actual run-time class. Field access and static method calls instead use:
- A. A random choice made at class-loading time
- B. Static binding, based on the declared (compile-time) type of the reference expression
- C. Dynamic binding, based on the object's run-time class, exactly like instance methods
- D. Dynamic binding, but only when the field is declared public
Correct answer: B. Static binding, based on the declared (compile-time) type of the reference expression
Unlike overridden instance methods, Java resolves field access and static methods purely from the reference's compile-time declared type, never from the object's actual run-time class.
More OOP with C++ and Java questions
- 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()) {…
- Which statement about C++ vtables is correct for a class with multiple virtual functions?