ProgrammingHardProduct-company placement · Java

What is printed?

What is printed?
int[] a = {3, 1, 2};
Integer[] b = {3, 1, 2};
System.out.print(Arrays.asList(a).size() + " " + Arrays.asList(b).size());
  1. A. 3 3
  2. B. 1 1
  3. C. 3 1
  4. D. 1 3

Correct answer: D. 1 3

Generics cannot hold primitives, so Arrays.asList(int[]) treats the whole int[] as ONE element (a List<int[]>). With Integer[] the array itself becomes the varargs array, giving 3 elements.

More OOP with C++ and Java questions

Exams that ask this topic