Question 1
What is the output of the following program? ```java import java.time.*; public class Main { public static void main(String[] args) { Period p = Period.of(1, 15, 40); Period n = p.normalized(); System.out.println(n.getYears() + " " + n.getMonths() + " " + n.getDays()); } } ```
A. 1 15 40
This is the raw un-normalized Period as stored by `Period.of(1, 15, 40)`. The candidate may believe `normalized()` is a no-op or that `Period.of` itself normalizes on construction. Neither is true: `Period.of` stores the given values as-is, and `normalized()` does adjust months, so the output differs from the original.
B. 2 4 10
Assumes `normalized()` also normalizes days using a 30-day month: 40 days would become 1 extra month plus 10 days, pushing months to 4 and days to 10. The Javadoc is unambiguous: 'The days unit is not normalized' — only the years and months components are adjusted.
C. Throws DateTimeException
Neither `Period.of` nor `normalized()` validates or restricts the magnitude of any field. Any integer is accepted for years, months, or days; months greater than 11 or days greater than 30 cause no exception — they are simply stored and then adjusted as needed by `normalized()`.
D. 2 3 40Correct answer
`normalized()` adjusts months so their absolute value is less than 12, carrying the overflow into years: 15 months becomes 1 additional year plus 3 remaining months, so years = 1 + 1 = 2 and months = 3. The Javadoc explicitly states that the days component is left unchanged, so 40 days passes through as-is.
Explanation
`Period.normalized()` adjusts only the years and months components so that the absolute value of months is less than 12, carrying surplus months into years; the days component is explicitly excluded from normalization. `Period.of` accepts any integer value for any component without restriction, and neither method throws. Carrying 15 months as 1 extra year and 3 remaining months, then adding to the initial 1 year, gives 2 years and 3 months — with 40 days left exactly as stored.