ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
// static method of the main class:
static void f(Object... a) {
    System.out.print(a == null ? "N " : a.length + " ");
}
// in main:
f(); f(null); f((Object) null); f(1, null);
  1. A. 0 1 1 2
  2. B. 0 N N 2
  3. C. 0 N 1 2
  4. D. 1 N 1 2

Correct answer: C. 0 N 1 2

f() passes an empty array. A bare null is assignable to Object[] itself, so it is passed as the array (a == null). Casting to Object forces varargs wrapping into a 1-element array; f(1, null) gives 2 elements.

More OOP with C++ and Java questions

Exams that ask this topic