Return to site

JAVA CERTIFICATION QUESTION: Java andThen methods for consumers and functions

· java

In Java, a Consumer method’s role is to execute a side effect such as printing to the console, logging an event, or writing to a database.

Given the following two classes:

import java.util.function.Consumer;
import java.util.function.Function;
class Value {
  static Integer counterC = 1;
  static Integer counterF = 1;
}
public class ChainTest {
  public static void main(String[] args) {
    Consumer<Integer> addC = i -> Value.counterC += i;
    Consumer<Integer> showC = i -> System.out.print(i);
    addC.andThen(showC).accept(1);

    Function<Integer, Integer> addF = i -> Value.counterF += i;
    Function<Integer, Integer> showF = i -> {
      System.out.print(i); return i;
    };
    addF.andThen(showF).apply(1);
  }
}

What is the output? Choose one.

A. 11

B. 12

C. 21

D. 22

E. There is no output.

 

 

 

 

 

·ꓭ uoᴉʇdo sᴉ ɹǝʍsuɐ ʇɔǝɹɹoɔ ǝɥꓕ ·uoᴉsnʅɔuoꓛ