ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
List<String> a = Arrays.asList("x", null);
System.out.print(a.size() + " ");
List<String> b = List.of("x", null);
System.out.print(b.size());
  1. A. "2 " is printed, then a NullPointerException is thrown
  2. B. 2 2
  3. C. 1 1
  4. D. Compile-time error

Correct answer: A. "2 " is printed, then a NullPointerException is thrown

Arrays.asList accepts null elements, but the List.of factory methods reject nulls with a NullPointerException at run time.

More OOP with C++ and Java questions

Exams that ask this topic