Trace through the execution of the following code snippet, t…
Trace through the execution of the following code snippet, then answer the four questions below about this execution. The Generator type is defined as follows: public interface Generator { public T generate(); } Code to trace (assume that the code is correctly defined inside a method): List gens = new LinkedList ();gens.add(() -> 5);gens.add(new Generator() { int[] vals = new int[] {1, 2}; int callNum = 0; @Override public Integer generate() { return vals[callNum++ % 2]; }});int sum = gens.get(0).generate();for (int i = 0; i < 3; i++) { sum += gens.get(1).generate();}System.out.println(sum); a. How many instances of the Generator type are created overall? b. For the Generator instance with the lowest number of calls to its generator method, how often is that instance's generator method called? c. For the Generator instance with the highest number of calls to its generator method, how often is that instance's generator method called? d. Which number will be printed by the code?
Read Details