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…
Question
Options
- A300.00
- BCellPhone,ToyCar,Motor,Fan
- C100.00, CellPhone,ToyCar,Motor,Fan
- D400.00
- ECellPhone,ToyCar
How the community answered
(54 responses)- A57% (31)
- B4% (2)
- C13% (7)
- D6% (3)
- E20% (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()withgetMax()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 thegetMax()value; additionally, E only includes two names, ignoring thatinstanceof Productmatches all four items becauseElectricProductIS-AProductthrough inheritance. - Note: the code as written has a compilation error -
a.price()calls a non-existent method (priceis a field, not a method); the intended expression isa.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
Community Discussion
No community discussion yet for this question.