Given the code fragment:
What is the result?
A. An exception is thrown at runtime
B. 07-31-2014
C. 2014-07-31
D. 2014-09-30
Given the code fragment:
What is the result?
A. An exception is thrown at runtime
B. 07-31-2014
C. 2014-07-31
D. 2014-09-30
Option : C
2014-07-31
The method of(int, int, int, int, int) is undefined for the type LocalDateTime
sorry!
Aside from the many unintentional compiler errors, everything here is legal. The trick being, pretending to change the value of “dt”.
Answer C:
Date classes are immutable, meaning that any manipulations on its objects need to be reassigned to the original reference variable, or to a new reference variable; else, the result of the manipulation will be ignored and lost.
The “dt.plusDays(30)” as well as the “dt.plusMonths(1)” lines were both executed nicely, and they have both returned new values, but these values are actually new objects! Therefore, “dt”‘s original value has never been manipulated, and the result remains the same.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main (String[] args){
LocalDateTime dt = LocalDateTime.of(2014, 07, 31, 1, 1);
dt.plusDays(30);
dt.plusMonths(1);
System.out.println(dt.format(DateTimeFormatter.ISO_DATE));
}
}
C