ProgrammingVery hardAmazon/Microsoft SDE MCQ · Java placement

What is printed?

What is printed?
class P { static String s() { return "S"; } }
// in main:
P p = null;
System.out.print(p.s());
  1. A. S
  2. B. null
  3. C. It throws NullPointerException
  4. D. Compile error: static method via instance

Correct answer: A. S

A static method called through an expression uses only the expression's static type; the reference is never dereferenced, so no exception is thrown and S is printed. The compiler merely warns.

More OOP with C++ and Java questions

Exams that ask this topic