Oracle Certified Professional Java Se 8 Programmer · Free Practice Question Easy
Question 44
Given code of Test.java file:
- package com.udayan.ocp;
- import java.time.*;
- public class Test {
- public static void main(String [] args) {
- System.out.println(Duration.ofDays(-2));
- }
- }
What will be the result of compiling and executing Test class?
- A P-2D
- B PT48H
- C PT-48H
- D P2D
Reveal correct answer
Correct answer: C
Explanation
As Period is expressed in terms of Years, Months and Days, Duration is expressed in terms of Hours, Minutes and Seconds.
String representation of Period starts with "P" whereas String representation of Duration starts with "PT".
You should be aware of following 'of' methods for the exam:
Duration.ofDays(long days) => Returns a Duration instance with specified number of days converted to hours. -2 days equals to -48 hours.
Duration.ofHours(long hours) => Returns a Duration instance with specified number of hours.
Duration.ofMinutes(long minutes) => Returns a Duration instance with specified number of minutes.
Duration.ofSeconds(long seconds) => Returns a Duration instance with specified number of seconds, nanos field is set to 0. NOTE: if nanos field is 0, toString ignores it.
Duration.ofMillis(long millis) => Returns a Duration instance with passed value converted to seconds and nano seconds.
Duration.ofNanos(long nanos) => Returns a Duration instacne with passed value converted to seconds and nono seconds.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
