ProgrammingHardProduct-company placement · Java
What happens when C is compiled?
What happens when C is compiled?
class P { static void s() { } }
class C extends P {
@Override static void s() { }
}- A. Compile-time error: s() does not override a superclass method
- B. It compiles; C.s() overrides P.s()
- C. It compiles with a warning; C.s() hides P.s()
- D. Compile-time error: static methods cannot be redeclared
Correct answer: A. Compile-time error: s() does not override a superclass method
Static methods are hidden, never overridden. Without @Override the code compiles (hiding); the annotation makes the compiler check for a real override and fail.
More OOP with C++ and Java questions
- Which lazily-initialised singleton is thread-safe WITHOUT any explicit synchronization or volatile field?
- What is printed? java // static generic method of the main class: static <T extends Comparable<T>> T max(T a, T b) { return a.compareTo(b)…
- What is printed? java class A { private int v = 1; boolean same(A other) { return v == other.v; } void set(A other) { other.v = 9; } } //…
- What is printed? cpp struct M { M(const char s) { cout << s; } }; struct S { M x, y; S() : y("Y"), x("X") { cout << "S"; } }; int main() {…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { cout << "~D "; } void…
- What is printed? cpp struct E { virtual const char what() const { return "E"; } }; struct F : E { const char what() const override {…