Question 17
Back to Exercise
What is the output of the following Java program?
import java.util.Comparator;
import java.util.List;
public class Main {
public static void main(String[] args) {
record Score(String name, int total) { } // local record: visible only here
List<String> raw = List.of("Asha:45,40", "Ravi:30,38", "Meena:48,47");
Score best = raw.stream()
.map(s -> s.split(":"))
.map(p -> new Score(p[0], List.of(p[1].split(",")).stream().mapToInt(Integer::parseInt).sum()))
.max(Comparator.comparingInt(Score::total))
.orElseThrow();
System.out.println(best + " " + best.total());
}
}
- A Score[name=Meena, total=95] 95 Correct
- B Score[name=Asha, total=85] 85
- C Meena 95
- D Compilation error
Correct Answer: Option A
Local records are handy for intermediate results inside one method.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!