← All 1Z0-819 Flashcard Decks

Lambda Expressions Flashcards

7 cards from real 1Z0-819 practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.

Read the first 7 Lambda Expressions flashcards as text
  1. What is the target type of a lambda expression?

    Answer: The functional interface type inferred from context

    The target type is the functional interface that the compiler infers from the assignment, method argument, or cast context where the lambda appears.

  2. Given: Consumer c1 = s -> System.out.print(s); Consumer c2 = s -> System.out.print(s.length()); Consumer combined = c1.andThen(c2); combined.accept("Hi"); — what is printed?

    Answer: Hi2

    Consumer.andThen chains consumers in order, so c1 prints "Hi" then c2 prints "2", producing "Hi2".

  3. Which of the following correctly uses a constructor reference?

    Answer: Supplier> s = ArrayList::new;

    ArrayList::new is a valid constructor reference that matches the Supplier> functional interface because ArrayList has a no-arg constructor.

  4. In Java 11, which method of Function allows composing a function BEFORE the current one?

    Answer: compose

    Function.compose(before) returns a composed function that applies 'before' first and then applies the current function to the result.

  5. What happens if you assign the same lambda body to two different compatible functional interface variables?

    Answer: It works; the lambda adopts each interface's target type independently

    A lambda expression is poly-expression and can be assigned to any compatible functional interface; each assignment creates an independent instance.

  6. Which statement about Comparator.comparing() used with a lambda is TRUE?

    Answer: It returns a Comparator built from a key-extraction Function.

    Comparator.comparing(Function keyExtractor) creates a Comparator that compares by the extracted key, commonly used with lambdas or method references.

  7. What is the output of: IntStream.of(1,2,3).map(x -> x * x).sum();

    Answer: 14

    The lambda squares each element (1, 4, 9) and sum() adds them: 1+4+9=14.