What is the result?

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<Product> 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?

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<Product> 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?

A.
2 : 30

B.
4 : 0

C.
4 : 60

D.
4 : 60
2 : 30
3 : 20
1 : 10

E.
The program prints nothing.



Leave a Reply 6

Your email address will not be published. Required fields are marked *


smh

smh

if you comment out: products.add(p);
then answer is A. 2 : 30
the question has something missing.

DG

DG

The answer should be 4:70 as per the provided code sample.
However, there is no such option.

smh

smh

C. 4 : 60

if you we correct first line of code as
List products = new ArrayList(Arrays.asList(new

smh

smh

please correct to
List products = new ArrayList(Arrays.asList(new Product(1, 10),
new Product (2, 30),
new Product (3, 20)));

andrei

andrei

products.add(p) throws UnsupportedOperationException.
All provided results are not correct.

smh

smh

after making small corrections in the code, products.add(p) works fine.