Java Duration and Period ISO-8601 Output Format
Given the code fragment: What is the result? - 
Community Votes
100% of anonymous learners picked answer B. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
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.
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.
Community Discussion (5 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The correct answer is B because the code prints the string representation ofDuration 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?
ISO-8601 standardizes duration representation. 5000 milliseconds is exactly 5 seconds, so it converts to the base unit 'S' prefixed by 'PT'.
Does Period include a 'T' separator like Duration?
No. Period uses only the 'P' prefix (e.g., P6D) because it represents calendar-based dates. Duration uses 'PT' to separate the date part from the time part.