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