ProgrammingHardProduct-company placement · GATE CS
How many of these statements about Java methods are correct? 1. A final method can be overloaded in a subclass. 2. A static method can be overridden by a static method in a subclass. 3. A private method can be overridden in a subclass. 4. An overriding method may declare a subtype as its return type. 5. An overriding method may reduce the visibility, e.g. public to protected.
- A. 1
- B. 3
- C. 4
- D. 2
Correct answer: D. 2
1 is true (final stops overriding, not overloading) and 4 is true (covariant returns). A same-signature static method HIDES rather than overrides, private methods are not inherited, and narrowing visibility is a compile error.
More OOP with C++ and Java questions
- Classes A, B extends A and C extends B all override m(). Inside C, which way can call A's version of m() on the current object?
- Joshua Bloch recommends implementing a singleton as a single-element enum. Which advantage does this give over a class with a private…
- What is printed? cpp struct P { const char s; P(const char s) : s(s) { cout << s; } ~P() { cout << "~" << s; } }; struct B { P b{"B"}; }…
- What is printed? cpp struct B { virtual ~B() { hi(); } virtual void hi() { cout << "B"; } }; struct D : B { ~D() { hi(); } void hi()…
- What is printed? cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct D : B { void f(int x = 2) override { cout << "D"…
- What is printed? cpp struct B { void f(int) { cout << "Bi"; } void f(double) { cout << "Bd"; } }; struct D : B { void f(double) { cout <<…