Given:
How many LocalDate objects are created in this example?
Given:
How many LocalDate objects are created in this example?
In this example, the LocalDate class is immutable, meaning every method call that modifies the date returns a new LocalDate object. The code creates the following LocalDate objects: first, when 'd1' is initialized with the current date using LocalDate.now(); second, when d1.plusDays(1) is called and returns a new LocalDate object (though it isn't assigned to any variable, it is still created); third, when 'd1' is updated to d1.minusMonths(2); fourth, when 'd2' is assigned the value of d1.plusWeeks(3); fifth, when d2.minusDays(4) is called and returns a new LocalDate object (again, not assigned to any variable, but still created). Therefore, a total of 5 LocalDate objects are created.
Apparently I'm the only one answering this. BUT LocalDate is immutable so every method: plusDays(), minusMonths, plusWeeks, minusDays(). ALL return a new LocalDate Object. Resulting in 5 Objects being created. Also d1.plusDays(1) on the second line would do nothing as this return value is not caught.
create object it doesn't mean that you must have a reference to it LocalDate is an immutable date-time object So LocalDate d1 = LocalDate.now(); //1 d1.plusDays(1); //1 d1 = d1.minusMonths(2); //1 LocalDate d2 = d1.plusWeeks(3); //1 d2.minusDays(4); //1 d2 = null; 1+1+1+1+1=5 objects created
LocalDate ld = LocalDate.now(); create 1st d1 = d1.minusMonths(2); 2nd LocalDate d2 = d1.plusWeeks(3); 3rd
A: LocalDate ld = LocalDate.now(); - This line creates the first LocalDate object, which represents the current date. LocalDate ld2 = ld.plusWeeks(3); - This line creates the second LocalDate object. The plusWeeks method returns a new LocalDate object that represents the date three weeks after the date in ld. The other methods called on ld and ld2 (plusDays, minusMonths, minusDays) also return new LocalDate objects, but these are not assigned to any variables, so they are not accessible after the method call and are eligible for garbage collection.
Correction: B = 3 => null line and d1.plus... not is asigned so no count
B. 3 Explanation: 1. Initially, d1 is assigned the current date using LocalDate.now(), creating the first LocalDate object. 2 d1.plusDays(1) returns a new LocalDate object that is one day ahead of d1. However, this new object is not assigned to any variable, so it is not counted as a created object. 3. d1 = d1.minusMonths(2) assigns a new LocalDate object that is two months before d1 to d1. This creates the second LocalDate object. 4. d1.plusWeeks(3) returns a new LocalDate object that is three weeks ahead of d1. However, this new object is not assigned to any variable, so it is not counted as a created object. 5. d2.minusDays(4) does not create a new LocalDate object because the return value is not assigned to any variable. 6. d2 = null assigns null to d2, but it does not create a new LocalDate object. Therefore, the total number of LocalDate objects created is 3.
Option A is correct
LocalDate is immutable (like String for example) and does not have public constructor but factory methods. When we create an obj, the proprieties cannot change. For example: d1- now... today in other words d1.plusDays(1)...tomorrow (...today is still today) d1.minusMonth(2)...is the same date but two months ago...and so on.
LocalDate d1 = LocalDate.now(); //1 object created with the factory method now() d1.plusDays(1); //1 object created with instance method plusDays() d1 = d1.minusMonth(2); //1 object created with instance method minusMonth() LocalDate d2 = d1.plusWeeks(3); // 1 object with instance method plusWeeks() d2.minusDays(4); // 1 object with instance method minusDays() d2 = null// no object is created.
The correct answer is **A. 2**. In this example, two `LocalDate` objects are created. The first one is created when `d1` is initialized with the current date using the `now()` method. The second one is created when `d2` is assigned the value of `d1.plusWeeks(3)`. The other method calls, such as `plusDays(1)`, `minusMonths(2)`, and `minusDays(4)` do not create new objects because the `LocalDate` class is immutable and these methods return new objects instead of modifying the existing ones.
The correct answer is **A. 2**. In this example, two `LocalDate` objects are created. The first one is created when `d1` is initialized with the current date using the `now()` method. The second one is created when `d2` is assigned the value of `d1.plusWeeks(3)`. The other method calls, such as `plusDays(1)`, `minusMonths(2)`, and `minusDays(4)` do not create new objects because the `LocalDate` class is immutable and these methods return new objects instead of modifying the existing ones.
i think 3 (80%) b