1z0-829 Exam QuestionsBrowse all questions from this exam

1z0-829 Exam - Question 4


Given the code fragment:

What is the result?

Show Answer
Correct Answer: A

The method truncatedTo(ChronoUnit.MINUTES) truncates the Instant object to the nearest minute. Given the loginTime of 2021-01-12T21:58:18.817Z and the logoutTime of 2021-01-12T21:58:19.880Z, both will be truncated to 2021-01-12T21:58:00Z. After truncation, loginTime and logoutTime are equal, so logoutTime.isAfter(loginTime) will return false, and the else statement will execute, printing 'Can't logout'.

Discussion

5 comments
Sign in to comment
SampsOption: A
Feb 10, 2024

A is correct

james2033Option: A
Feb 11, 2024

package q04; import java.time.Instant; import java.time.temporal.ChronoUnit; public class Q04 { public static void main(String[] args) throws InterruptedException { // Login time: 2021-01-12T21:58:18.817Z Instant loginTime = Instant.now(); Thread.sleep(1000); // Logout time: 2021-01-12T21:58:19.880Z Instant logoutTime = Instant.now(); loginTime = loginTime.truncatedTo(ChronoUnit.MINUTES); // line n1 logoutTime = logoutTime.truncatedTo(ChronoUnit.MINUTES); if (logoutTime.isAfter(loginTime)) { System.out.println("Logged out at: " + logoutTime); } else { System.out.println("Can't logout"); } } } // Result: // Can't logout

minhdevOption: A
May 5, 2024

A is correct: truncatedTo() coverts the logoutTime=LoginTime => else is reached.

xplorerpjOption: A
Jun 15, 2024

A is correct

UtemanOption: A
Jul 2, 2024

Outputs Can't logout