ProgrammingVery hardProduct-company placement · Java
What is printed?
What is printed?
System.out.print(String.valueOf((Object) null) + " "); System.out.print(String.valueOf(null));
- A. null null
- B. Compile-time error: ambiguous call
- C. A NullPointerException is thrown before anything is printed
- 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
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(Integer a…
- What is printed? java // static methods of the main class: static void f(double d) { System.out.print("D"); } static void f(Object o) {…
- Which line fails to compile? java // static methods of the main class: static void g(long l) { } static void h(Long l) { } // in main…
- Which lines fail to compile? java String s = "x"; Number n = 1; Runnable r = () -> { }; Object o = s; boolean a = s instanceof Integer; //…
- What is printed? java // Java 16 or later record R(int[] a) { } record S(List<Integer> l) { } // in main: System.out.print(new R(new…
- What is printed? java enum Op { ADD { int apply(int a, int b) { return a + b; } }, MUL { int apply(int a, int b) { return a b; } }…