1Z0-809 · Question #209
Given the code fragment: `` LocalTime now = LocalTime.now(); long timeToBreakfast = 0; LocalTime office_start = LocalTime.of(7, 30); if (office_start.isAfter(now)) { timeToBreakfast =…
The correct answer is C. 60. Since office_start (7:30) is after now (6:30), isAfter() returns true, so the if branch executes - computing now.until(office_start, MINUTES), which is exactly 60 minutes from 6:30 to 7:30, making C correct. A is wrong because the code is syntactically and semantically valid…
Question
LocalTime now = LocalTime.now();
long timeToBreakfast = 0;
LocalTime office_start = LocalTime.of(7, 30);
if (office_start.isAfter(now)) {
timeToBreakfast = now.until(office_start, MINUTES);
} else {
timeToBreakfast = now.until(office_start, HOURS);
}
System.out.println(timeToBreakfast);
Assume that the value of now is 6:30 in the morning.
What is the result?Options
- AAn exception is thrown at run time.
- B0
- C60
- D1
How the community answered
(44 responses)- A9% (4)
- B16% (7)
- C70% (31)
- D5% (2)
Explanation
Since office_start (7:30) is after now (6:30), isAfter() returns true, so the if branch executes - computing now.until(office_start, MINUTES), which is exactly 60 minutes from 6:30 to 7:30, making C correct.
A is wrong because the code is syntactically and semantically valid - LocalTime, until(), and ChronoUnit.MINUTES/HOURS are all standard Java API members that work without error here. B (0) is wrong because now and office_start differ by a full 60 minutes, so the result is never zero. D (1) is wrong because the else branch (which would compute now.until(office_start, HOURS) = 1 hour) is never reached - the if condition is true, so we stay in the if block.
Memory tip: Trace the boolean condition first before looking at the math - isAfter(now) asks "is office_start later than now?", and since 7:30 > 6:30 the answer is yes, locking you into the MINUTES branch. Once you know the branch, the unit (MINUTES) makes the arithmetic straightforward: 7:30 minus 6:30 = 60 minutes.
Community Discussion
No community discussion yet for this question.