Java OCA OCP Practice Question 996

Question

What is the result of the following code?

1: interface MyClass { 
2:   boolean test(int height, int limit); 
3: } /*from  w w w.  j  av a 2s  .  c o m*/
4:  
5: public class Main { 
6:   public static void main(String[] args) { 
7:   check((h, l) -> h.append(l).isEmpty(), 5); 
8:   } 
9:   private static void check(MyClass climb, int height) { 
10:    if (climb.test(height, 10))  
11:      System.out.println("too high"); 
12:    else  
13:      System.out.println("ok"); 
14:  } 
15: } 
  • A. ok
  • B. too high
  • C. Compiler error on line 7.
  • D. Compiler error on line 10.
  • E. Compiler error on a different line.
  • F. A runtime exception is thrown.


C.

Note

The interface takes two int parameters.

The code on line 7 attempts to use them as if one is a StringBuilder.

It is tricky to use types in a lambda when they are implicitly specified.

Remember to check the interface for the real type.




PreviousNext

Related