Question 1
What does this print? ```java import java.util.function.*; public class Main { public static void main(String[] args) { Consumer<StringBuilder> addX = sb -> sb.append("x"); Consumer<StringBuilder> addY = sb -> sb.append("y"); StringBuilder box = new StringBuilder(); addX.andThen(addY).accept(box); System.out.println(box); } } ```
A. xyCorrect answer
Correct. andThen runs the receiver (append "x") first and then the after-consumer (append "y") on the same StringBuilder, so it holds "xy" (Consumer.andThen).
B. yx
Reverses the order: andThen runs the receiver first, not the after-consumer first, so "yx" is wrong.
C. x
Assumes only the receiver runs; andThen guarantees the after-consumer runs too, so the "y" is not skipped.
D. y
Assumes only the after-consumer runs; the receiver is not skipped, so the "x" is still appended.
Explanation
Consumer.andThen returns a new Consumer that runs the receiver first and then the supplied after-consumer, both acting on the same argument. Because a Consumer returns nothing, no value passes between the two stages; the side effects simply apply left to right to the one shared StringBuilder.