ProgrammingVery hardProduct-company placement · Java

What is printed?

What is printed?
System.out.print(String.valueOf((Object) null) + " ");
System.out.print(String.valueOf(null));
  1. A. null null
  2. B. Compile-time error: ambiguous call
  3. C. A NullPointerException is thrown before anything is printed
  4. D. "null " is printed, then a NullPointerException is thrown

Correct answer: D. "null " is printed, then a NullPointerException is thrown

With an explicit Object argument, valueOf(Object) returns "null". A bare null picks the most specific overload, valueOf(char[]), which tries to read the array and throws NullPointerException.

More OOP with C++ and Java questions

Exams that ask this topic