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