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 onClick(() -> { C onClick(() -> D); E }); F afterDelay(1, () -> { G onKeyPress(() -> { H }); I }); J }); K onKeyPress(() -> { L onKeyPress(() -> { M onKeyPress(() -> N); O }); P afterDelay(2, () -> Q); R }); S onKeyPress(() -> { T twice(() -> { U twice(() -> { V }); W }); X afterDelay(2, () -> Y); Z }); AA afterDelay(3, () -> { AB onClick(() -> { AC onClick(() -> AD); AE }); AF afterDelay(1, () -> { AG twice(() -> AH); AI }); AJ }); AK afterDelay(1, () -> { AL afterDelay(1, () -> AM); AN onKeyPress(() -> { AO onClick(() -> AP); AQ }); AR }); AS
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):
CLICK CLICK TICK KEY TICK KEY TICK CLICK TICK TICK TICK TICK KEY KEY TICK TICK
A CLICK CLICK TICK KEY L T U V V U V V TICK AL KEY L T U V V U V V M AO TICK CLICK AP TICK AB Q Y AM TICK B Q Y TICK AG AH AH TICK G KEY L T U V V U V V M AO M N H KEY L T U V V U V V M AO M N H M N N TICK TICK AFTER K S AA AK AS P R W W X Z AN AR P R W W X Z O AQ AF AJ F J AI I P R W W X Z O AQ O P R W W X Z O AQ O O
Consider the following code:
public class StatefulPuzzle {
private int x = 0;
public void run() {
onClick(() -> {
x++;
System.out.println("x=" + x);
});
onClick(() -> {
if (x < 1) {
System.out.println("Hi!");
}
});
if (x > 0) {
onClick(() -> {
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: