Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 36

Given:

public class product {

int id; int price;

public Product (int id, int price) {

this.id = id;

this.price = price;

}

public String toString() { return id + ":" + price; }

}

and the code fragment:

List products = Arrays.asList(new Product(1, 10), new Product (2, 30), new Product (2, 30));

Product p = products.stream().reduce(new Product (4, 0), (p1, p2) -> { p1.price+=p2.price; return new Product (p1.id, p1.price);}); products.add(p); products.stream().parallel()

.reduce((p1, p2) - > p1.price > p2.price ? p1 : p2)

.ifPresent(System.out: :println);

What is the result?

    Correct Answer: E

    The program will throw an UnsupportedOperationException because Arrays.asList returns a fixed-size list, and attempting to add a new element to this list is not supported.

Discussion
rameasy

When the ArrayList is used instead of Arrays.asList. The result is 4 : 70.

adnano1234Option: E

Arrays.asList return a Arrays type. It has fix-sized array so not support add operation. It return an UnSupportedOpertationException

Svetleto13Option: C

If you change the code like that: List<Product> products = new ArrayList<>(Arrays.asList(new Product(1, 10), new Product (2, 20), new Product (2, 30))); answer is C 4:60

steefaand

There is no correct answer. Arrays.asList doesn't allow adding of new elements. If that was corrected answer would be 4 : 70. Tested both cases.

r1muka5Option: C

Fixing compilation errors and using new ArrayList() would result in 4:70

WilsonKKerllOption: E

Sorry 4 : 70 is error. The answer is java.lang.UnsupportedOperationException. Becource Arrays.asList() is not java.util.ArrayList, you should add List<> newList = new ArrayList<>(products); newList.add(p); So output is 4 : 70 .

WilsonKKerll

Answer is 4:70......

DestroyerOption: B

Arrays.asList return a Arrays type. It has fix-sized array so not support add operation. It return an UnSupportedOpertationException