ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
// static methods of the main class:
static void f(Object o) { System.out.print("Object"); }
static void f(String s) { System.out.print("String"); }
// in main:
f(null);
  1. A. NullPointerException at run time
  2. B. Compilation error: ambiguous call
  3. C. Object
  4. D. String

Correct answer: D. String

null fits both parameter types, and Java picks the most specific applicable overload. String is a subclass of Object, so f(String) wins. Adding an unrelated overload such as f(Integer) would make the call ambiguous.

More OOP with C++ and Java questions

Exams that ask this topic