Java OCA OCP Practice Question 3035

Question

Choose the correct option based on this code segment:

List<Integer> ints = Arrays.asList(1, 2, 3, 4, 5);
ints.replaceAll(i -> i * i); // LINE
System.out.println(ints);
  • a) this code segment prints: [1, 2, 3, 4, 5]
  • b) this program prints: [1, 4, 9, 16, 25]
  • c) this code segment throws java.lang.UnsupportedOperationException
  • d) this code segment results in a compiler error in the line marked with the comment LINE


b)

Note

the replaceAll() method (added in Java 8 to the List interface) takes an UnaryOperator as the argument.

In this case, the unary operator squares the integer values.

hence, the program prints [1, 4, 9, 16, 25].

the underlying List object returned by Arrays.asList() method can be modified using the replaceAll() method and it does not result in throwing java.lang.UnsupportedOperationException.




PreviousNext

Related