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(3, () -> { B afterDelay(1, () -> { C twice(() -> D); E }); F onClick(() -> { G twice(() -> { H }); I }); J }); K afterDelay(4, () -> { L twice(() -> { M twice(() -> { N }); O }); P afterDelay(1, () -> { Q onKeyPress(() -> { R }); S }); T }); U afterDelay(1, () -> { V afterDelay(1, () -> { W twice(() -> { X }); Y }); Z twice(() -> AA); AB }); AC onClick(() -> { AD twice(() -> { AE onClick(() -> AF); AG }); AH afterDelay(1, () -> AI); AJ }); AK onKeyPress(() -> { AL onKeyPress(() -> { AM onKeyPress(() -> { AN }); AO }); AP afterDelay(1, () -> { AQ onKeyPress(() -> { AR }); AS }); AT }); AU
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 KEY TICK CLICK KEY TICK CLICK KEY CLICK TICK TICK TICK CLICK TICK TICK TICK
A TICK KEY AL TICK V AA AA CLICK AD AE AE KEY AL AM TICK AQ CLICK AD AE AE AF AF KEY AL AM AM AN AR CLICK AD AE AE AF AF AF AF TICK B W X X AI AQ TICK L M N N M N N AI AQ AI TICK C D D CLICK AD AE AE AF AF AF AF AF AF G H H TICK Q TICK AI TICK AFTER K U AC AK AU AP AT Z AB AG AG AH AJ AP AT AO AS AG AG AH AJ AP AT AO AO AG AG AH AJ F J Y AS O O P T AS E AG AG AH AJ I S
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: