Exam 1Z0-809: Java SE 8 Programmer II

Java SE 8 Programmer II Exam Question

Question

Given the code fragment: ZonedDateTime depart = ZonedDateTime.of(2015, 1, 15, 3, 0, 0, 0, ZoneID.of("UTC-7")); ZonedDateTime arrive = ZonedDateTime.of(2015, 1, 15, 9, 0, 0, 0, ZoneID.of("UTC-5")); long hrs = ChronoUnit.HOURS.between(depart, arrive); //line n1 System.out.println("Travel time is" + hrs + "hours"); What is the result?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The code creates two ZonedDateTime objects, depart and arrive, representing the departure and arrival times of a flight, respectively.

depart is created with the date and time of January 15th, 2015 at 3:00 AM UTC-7, which is 7 hours behind UTC. arrive is created with the same date and time of January 15th, 2015, but at 9:00 AM UTC-5, which is 5 hours behind UTC.

The ChronoUnit.HOURS.between() method is used to calculate the difference between depart and arrive in hours. The result of this calculation is stored in the hrs variable.

The between() method returns the amount of time between two temporal objects in terms of the specified unit, which in this case is hours. The result is calculated by considering the difference in the timeline of the two time zones.

So, to calculate the travel time between depart and arrive, we need to find the elapsed time between depart and arrive in hours, accounting for the difference in time zones.

The time difference between UTC-7 and UTC-5 is 2 hours. Therefore, the elapsed time between depart and arrive is 6 hours (9:00 AM - 3:00 AM - 2 hours).

Therefore, the output of the program will be: "Travel time is 6 hours".

Therefore, the correct answer is B.