Java OCA OCP Practice Question 3025

Question

Given this code snippet:

LocalDate dateOfBirth = LocalDate.of(1988, Month.NOVEMBER, 4);
MonthDay monthDay =
       MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
boolean ifTodayBirthday =
       monthDay.equals(MonthDay.from(LocalDate.now())); // COMPARE
System.out.println(ifTodayBirthday ? "Happy birthday!" : "Yet another day!");

Assume that today's date is 4th November 2015.

Choose the correct answer based on this code segment.

  • a) this code will result in a compiler error in the line marked with the comment COMPARE
  • b) When executed, this code will throw DateTimeException
  • c) this code will print: Happy birthday!
  • d) this code will print: Yet another day!


c)

Note

this code gets the month-and-day components from the given LocalDate and creates a MonthDay object.

another way to create a MonthDay object is to call the from() method and pass a LocalDate object.

the equals() method compares if the month and date components are equal and if so returns true.

Since the month and day components are equal in this code (assuming that the today's date is 4th November 2015 as given in the question), it results in printing "Happy birthday!".




PreviousNext

Related