Suppose we are in an environment where CLICK, TICK, and KEY events can occur, and we have the following functions available:
onClick(closure)
Executesclosureafter every subsequentCLICKevent.
onKeyPress(closure)
Executesclosureafter every subsequentKEYevent.
afterDelay(tickCount, closure)
Executesclosureonce after exactlytickCountTICKevents have occurred.
twice(closure)
Immediately executes closure two times.
Multiple closures can be registered to handle the same event. If this happens, they execute in the order they were registered.
Consider the following code:
A afterDelay(4, () -> { B onKeyPress(() -> { C onClick(() -> { D }); E }); F afterDelay(1, () -> { G onKeyPress(() -> { H }); I }); J }); K afterDelay(2, () -> { L onClick(() -> { M twice(() -> { N }); O }); P afterDelay(1, () -> { Q twice(() -> { R }); S }); T }); U afterDelay(1, () -> { V afterDelay(1, () -> W); X twice(() -> { Y onKeyPress(() -> { Z }); AA }); AB }); AC onKeyPress(() -> { AD afterDelay(1, () -> { AE onClick(() -> { AF }); AG }); AH twice(() -> { AI onKeyPress(() -> AJ); AK }); AL }); AM onClick(() -> { AN onClick(() -> { AO twice(() -> { AP }); AQ }); AR afterDelay(2, () -> { AS twice(() -> AT); AU }); AV }); AW
Write out the order in which all the marked statements will execute given the following sequence of events (showing both the marked statements and the event names):
TICK TICK KEY TICK KEY CLICK TICK TICK CLICK KEY KEY TICK TICK CLICK TICK TICK
A TICK TICK V Y Y KEY AD AI AI Z Z TICK L KEY AD AI AI Z Z AJ AJ CLICK AN M N N TICK W AE TICK B Q R R AE CLICK AN M N N AO AP AP AF AF KEY AD AI AI Z Z AJ AJ AJ AJ C KEY AD AI AI Z Z AJ AJ AJ AJ C AJ AJ TICK AS AT AT TICK G AE AE CLICK AN M N N AO AP AP AF AF AO AP AP D D AF AF TICK AS AT AT TICK AFTER K U AC AM AW X AA AA AB AH AK AK AL P T AH AK AK AL AR AV O AG F J S AG AR AV O AQ AH AK AK AL E AH AK AK AL E AU I AG AG AR AV O AQ AQ AU
Consider the following code:
public class StatefulPuzzle {
private int x = 0;
public void run() {
onClick(() -> {
x++;
System.out.println("x=" + x);
});
if (x > 0) {
onClick(() -> {
System.out.println("Hi!");
});
}
onClick(() -> {
if (x < 2) {
System.out.println("Bye!");
}
});
System.out.println("End of run()");
}
}
Given a call to run() followed by two CLICK events, what would this code print?
End of run() x=1 Hi! Bye! x=2 Hi! Bye!
Related puzzles: