Java Duration and Period ISO-8601 Output Format

Answer Correct answer: B — The code outputs PT5SPT1MP6D because Duration.ofMillis(5000) becomes PT5S, Duration.ofSeconds(60) becomes PT1M, and Period.ofDays(6) becomes P6D.

Given the code fragment: What is the result? - image

  1. PT5000SPT60MP6D
  2. PT5SPT1MP6D Correct Answer
  3. 5S1M6D
  4. 5000S60M6D

Community Votes

B
100%

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)

gnilapon.ebenezaire 👍 1
ISO-8601 format
Uteman 👍 1
B is correct
minhdev 👍 2
Period: P#(Y,M,D) Duration: PT#(H, M, S) Result: PT5SPT1MP6D B is correct answer
james2033 👍 2 Selected: B
package 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
Samps 👍 1 Selected: B
B is correcct

Comments & Corrections

No comments yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

Submitted for moderation before publishing. Keep it helpful and respectful.

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?

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.

More 1Z0-829 FAQ →

Related Analysis

← Back to 1Z0-829 Study Guide