nerdexam
Oracle

1Z0-829 · Question #11

Given: class Product { String name; double price; Product(String s, double d) { this.name = s; this.price = d; } } class ElectricProduct extends Product { ElectricProduct(String name, double price)…

The correct answer is A. 300.00. Option A is correct because sts.getMax() returns the largest value from the DoubleSummaryStatistics collected over the prices {100, 80, 150, 300} - which is 300. All four items pass the instanceof ElectricProduct filter since the entire list is made up of ElectricProduct…

Working with Streams and Lambda expressions

Question

Given: class Product { String name; double price; Product(String s, double d) { this.name = s; this.price = d; } } class ElectricProduct extends Product { ElectricProduct(String name, double price) { super(name, price); } } and the code fragment: List<Product> p = List.of( new ElectricProduct("CellPhone",100), new ElectricProduct("ToyCar",80), new ElectricProduct("Motor",150), new ElectricProduct("Fan",300) ); DoubleSummaryStatistics sts = p.stream().filter(a -> a instanceof ElectricProduct) .collect(Collectors.summarizingDouble(a -> a.price())); String s1 = p.stream().filter(a -> a instanceof Product) .collect(Collectors.mapping(p2 -> p2.name, Collectors.joining(", "))); System.out.println(sts.getMax()); System.out.println(s1); What is the result?

Options

  • A300.00
  • BCellPhone,ToyCar,Motor,Fan
  • C100.00, CellPhone,ToyCar,Motor,Fan
  • D400.00
  • ECellPhone,ToyCar

How the community answered

(54 responses)
  • A
    57% (31)
  • B
    4% (2)
  • C
    13% (7)
  • D
    6% (3)
  • E
    20% (11)

Explanation

Option A is correct because sts.getMax() returns the largest value from the DoubleSummaryStatistics collected over the prices {100, 80, 150, 300} - which is 300. All four items pass the instanceof ElectricProduct filter since the entire list is made up of ElectricProduct instances.

Why the distractors are wrong:

  • C is wrong because 100.0 is the minimum price, not the maximum - mixing up getMin() with getMax() is the trap here.
  • D (400.00) doesn't correspond to any valid statistic - the sum is 630, the count is 4, and no operation on these prices yields 400.
  • B and E show names from the second stream (s1), not the getMax() value; additionally, E only includes two names, ignoring that instanceof Product matches all four items because ElectricProduct IS-A Product through inheritance.
  • Note: the code as written has a compilation error - a.price() calls a non-existent method (price is a field, not a method); the intended expression is a.price.

Memory tip: For instanceof checks, always think "IS-A" - a subclass instance passes a superclass instanceof test. And for DoubleSummaryStatistics, getMax() is simply the single largest value encountered in the stream.

Topics

#Streams & Collectors#Lambda expressions#DoubleSummaryStatistics#Polymorphism

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice