ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
class T {
    static String hi() { return "hi"; }
}
// in main:
T t = null;
System.out.print(t.hi());
  1. A. NullPointerException
  2. B. hi
  3. C. null
  4. D. Compilation error

Correct answer: B. hi

A static method call through a reference is compiled as T.hi(); the reference is never dereferenced, so its being null does not matter. (Calling an instance method on null would throw NullPointerException.)

More OOP with C++ and Java questions

Exams that ask this topic