ProgrammingVery hardHCL · Tech Mahindra pattern

What is printed?

What is printed?
class A { static void who() { System.out.print("A"); } }
class B extends A { static void who() { System.out.print("B"); } }
public class Main {
    public static void main(String[] args) {
        A a = new B();
        a.who();
    }
}
  1. A. A
  2. B. B
  3. C. Compilation error
  4. D. AB

Correct answer: A. A

Static methods are not polymorphic — calling a static method through a reference is resolved at compile time using the reference's declared type (A), not the object's runtime type. This is method hiding, not overriding, so A::who runs and prints A.

More OOP with C++ and Java questions

Exams that ask this topic