ProgrammingVery hardProduct company OA

A static method can be invoked through a reference variable that is null at run time without throwing NullPointerException. What is printed?

A static method can be invoked through a reference variable that is null at run time without throwing NullPointerException. What is printed?
class A {
    static String tag() { return "static-A"; }
}
// in main:
A a = null;
System.out.println(a.tag());
  1. A. It throws NullPointerException
  2. B. static-A
  3. C. null
  4. D. It throws a compile-time error

Correct answer: B. static-A

a.tag() is compiled to an invokestatic call resolved purely from the declared type A at compile time; the runtime value of a (even null) is never dereferenced, so no NPE occurs and "static-A" prints.

More OOP with C++ and Java questions

Exams that ask this topic