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

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 = new ArrayList <> (Arrays.asList(new Product(1, 10), new Product (2, 30), new Product (3, 20));

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: A

    First, the code creates a list of products with three initial `Product` objects. Then, it uses the `reduce` method to combine all products into a new `Product` object with id 4 and a combined price of all products (10+30+20 = 60). This `Product` object (4:60) is added to the list. Finally, the `reduce` method is used again to find the product with the highest price from the updated list and prints it, which is product 2:30. Therefore, the correct result printed by the program is '4:60' followed by '2:30'.

Discussion
Abdullah_RahahleahOption: A

Answer is A List<Product> products = new ArrayList<>(Arrays.asList(new Product(1, 10), new Product (2, 30), new Product (3, 20))); Product p = products.stream().reduce(new Product (4, 0), (p1, p2) -> { p1.price+=p2.price; return new Product (p1.id, p1.price);}); System.out.println(p); products.add(p); products.stream().parallel().reduce( (p1, p2) -> p1.price > p2.price ? p1 : p2).ifPresent(System.out::println);

Svetleto13Option: A

A,tested

asdfjhfgjuaDCVOption: A

A is the answer

steefaandOption: A

Answer is A assuming typos are corrected.

duydnOption: A

A is correct

WilsonKKerll

Another question: if the List<Product> like this-> List<Product> products = Arrays.asList(new Product(1, 10), new Product (2, 30), new Product (3, 20); And: products.add(p); Answer is compile error.

WilsonKKerllOption: A

Answer is A.