Java OCA OCP Practice Question 2262

Question

Which statements about the following application are true?

1:  package mypkg; 
2:  import java.util.function.*; 
3:  class Shape {} 
4:  public class Main { 
5:     public class Car { 
6:       int count; 
7:       public final Function<Shape,Main,Car> f = (h,w) -> new Car(); 
8:       public final IntSupplier<Integer> add = () -> count++; 
9:     } /*w ww  .j a va  2  s.c  om*/
10:    public static void main(String[] knight) { 
11:      final Car a = new Car(); 
12:      a.f.apply(new Shape(), new Main()); 
13:      a.add.getAsInt(); 
14:    } 
15: } 
  • I. The lambda expression for f on line 7 compiles without issue.
  • II. The lambda expression for add on line 8 compiles without issue.
  • III. Not counting the lambda expressions on lines 7 and 8, the code does not contain any compilation errors.
  • A. I only
  • B. I and II only
  • C. I, II, and III
  • D. II and III only
  • E. None of the above


E.

Note

The first statement is not true because Function takes two generic arguments and one input argument.

If BiFunction was used instead of Function on line 7, then the code would compile correctly.

The second statement is also not true because IntSupplier does not take any generic arguments.

The third statement is not true as well, since Car is an inner instance class.

Without an instance of Main in the static main() method, the call new Car() on line 11 does not compile.

For these reasons, all three statements are not true, making Option E the correct answer.




PreviousNext

Related