Java Duration and Period ISO-8601 Output Format
Short answer
B The code outputs PT5SPT1MP6D because Duration.ofMillis(5000) becomes PT5S, Duration.ofSeconds(60) becomes PT1M, and Period.ofDays(6) becomes P6D.
Question

Key Takeaways
The exam tests knowledge of the specific string formatting rules for ISO-8601 durations, specifically how milliseconds convert to seconds ('S') and how periods are prefixed with 'P'. The common trap is confusing the input units (milliseconds/seconds) with the output format or missing the 'T' separator.
This question tests the ability to predict the string output of Java's java.time.Duration and java.time.Period objects. It establishes that these classes implement toString() to return standardized ISO-8601 duration representations.
Common mistake: Candidates often select option C (5S1M6D) by stripping the 'PT' prefix and 'P' period marker, failing to recognize that the default toString() method includes these mandatory ISO-8601 components.
Expert Analysis
Why the Answer Is Correct
The correct answer is B because the code prints the string representation of Duration and Period objects using their toString methods. Duration.ofMillis(5000) equals 5 seconds, which formats as PT5S. Duration.ofSeconds(60) equals 1 minute, which formats as PT1M. Period.ofDays(6) formats as P6D. Concatenating these results in PT5SPT1MP6D.
Why the Other Options Are Wrong
Option A (PT5000SPT60MP6D) incorrectly retains the original numeric values (5000ms and 60s) instead of converting them to the base units (seconds and minutes) required by the ISO-8601 standard. Option C (5S1M6D) omits the mandatory 'PT' prefix for time-based durations and the 'P' prefix for date-based periods. Option D (5000S60M6D) mixes raw numbers with units but lacks the structural prefixes entirely.
Community Comment Notes
Community consensus strongly supports B, with users explicitly noting the ISO-8601 format structure. As one user noted, the result follows the pattern 'Period: P#(Y,M,D) Duration: PT#(H, M, S)', confirming that the prefixes are essential parts of the output.
Exam Strategy
Memorize the basic ISO-8601 duration patterns: 'PT' precedes time units (H, M, S), and 'P' precedes date units (Y, M, D). When printing Duration or Period objects in exams, always assume the full formatted string including prefixes unless explicitly asked otherwise.
Frequently Asked Questions
Why does Duration.ofMillis(5000) print as PT5S?
Does Period include a 'T' separator like Duration?
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 5 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 5
-
james2033 👍 2 Answered Bpackage q05; import java.time.Duration; import java.time.Period; public class Q05 { public static void main(String[] args) { Duration duration = Duration.ofMillis(5000); System.out.print(duration); duration = Duration.ofSeconds(60); System.out.print(duration); Period period = Period.ofDays(6); System.out.print(period); } } // Result: // PT5SPT1MP6D
-
minhdev 👍 2Period: P#(Y,M,D) Duration: PT#(H, M, S) Result: PT5SPT1MP6D B is correct answer
-
gnilapon.ebenezaire 👍 1ISO-8601 format
-
Samps 👍 1 Answered BB is correcct
-
Uteman 👍 1B is correct
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.