Java OCA OCP Practice Question 2989

Question

Given this code segment:

LocalDate joiningDate = LocalDate.of(2014, Month.SEPTEMBER, 20);
LocalDate now = LocalDate.of(2015, Month.OCTOBER, 20);
// GET_YEARS
System.out.println(years);
Which one of the following statements when replaced by the comment GET_YEARS will print 1 on the console?
a)   Period years = Period.between(joiningDate, now).getYears();
b)   Duration years = Period.between(joiningDate, now).getYears();
c)   int years = Period.between(joiningDate, now).getYears();
d)   Instant years = Period.between(joiningDate, now).getYears();


c)

Note

the between() method in Period returns a Period object.

the getYears() method called on the returned Period returns an int.

hence, option c) that declares years as int is the correct option.

Using the other three options will result in compiler errors because the getYears() method of Period return an int.




PreviousNext

Related