Site Search:

OCPJP Simulation Test 103

<Back to OCPJP





Question: What is the output of the following code?

  1 import java.util.function.*;
  2 public class Printing {
  3   public static void main(String...args) {
  4     Supplier<String> s1 = String::new;
  5     System.out.print(s1.get());
  6     Supplier<Integer> s2 = new Integer(0);
  7     System.out.print(s2.get());}}


Choice:
A. no output
B. empty space followed by 0
C. 0
D. null0
E. nullnull
F. s1s2
G. code does not compile
H. code compiles but throw runtime exceptions.








Answer: C

Explain: Supplier functional interface takes no parameter, returns an object of the defined type. Line 4 declared a Supplier returns an object of type String. String::new is the function reference, which returns a new empty String. Line 6 defined a Supplier returns an object of type Integer, The right side returns integer 0. Supplier functional interface defined the method get(), which is used to get the object supplied by the supplier.

Study:  Use  the built-in interfaces included in the java.util.function package such as Predicate, Consumer, Function, and Supplier   

Create and use ArrayList, TreeSet, TreeMap, and ArrayDeque objects