ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// static methods of the main class:
static void f(Object o)   { System.out.print("O"); }
static void f(Object[] a) { System.out.print("A"); }
// in main:
f(new String[]{"a"});
f((Object) new String[]{"a"});
f(new int[]{1});
  1. A. AAA
  2. B. AOO
  3. C. AOA
  4. D. OOO

Correct answer: B. AOO

String[] fits both, and Object[] is more specific than Object. The cast fixes the static type to Object. An int[] is not an Object[] (primitive arrays are not covariant), so only f(Object) applies.

More OOP with C++ and Java questions

Exams that ask this topic