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