Question 16
Back to Exercise
What is the output of the following Java program?
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
StringBuilder log = new StringBuilder();
List<Runnable> steps = new ArrayList<>();
steps.add(() -> log.append("validate;"));
steps.add(() -> log.append("save;"));
steps.add(0, () -> log.append("begin;"));
steps.add(() -> log.append("commit;"));
steps.forEach(Runnable::run);
System.out.println(log + " " + steps.size());
}
}
- A begin;validate;save;commit; 4 Correct
- B validate;save;begin;commit; 4
- C begin;validate;save;commit; 3
- D Compilation error
Correct Answer: Option A
Behaviour stored in a list runs in list order; Runnable::run is an unbound method reference.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!