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