Java OCA OCP Practice Question 1733

Question

What is the result of the following?

class Employee {/*from   ww  w .  j ava 2  s . co m*/
   private String name;
   private int empNum;
   private int score;


   public Employee(String name, int empNum, int score) {
      this.name = name;
      this.empNum = empNum;
      this.score = score;
   }
   // all getters and setters
}

public class Main {
   public static void main(String[] args) {
      Stream<Employee> ballots = Stream.of(
         new Employee("Jack", 1, 10),
         new Employee("Tom", 1, 8),
         new Employee("Jack", 2, 9),
         new Employee("Tom", 2, 8)
      );


      Map<String, Integer> scores = ballots.collect(
         groupingBy(Employee::getName, summingInt(Employee::getScore))); // w1
      System.out.println(scores.get("Jack"));
  }
}
  • A. The code prints 2.
  • B. The code prints 19.
  • C. The code does not compile due to line w1.
  • D. The code does not compile due to a different line.


B.

Note

This code compiles.

It creates a stream of Employee objects.

Then it creates a map with the contestant's name as the key and the sum of the scores as the value.

For Jack, this is 10 + 9, or 19, so Option B is correct.




PreviousNext

Related