What will be the output when the following code is executed?public class Product {public static void main(String[] args) { LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01);LocalDate tempLocalDate = startLocalDate;startLocalDate.plusDays(10);System.out.println(tempLocalDate);}} 2016-06-0101-06-20162016-06-1111-06-2016
Question
What will be the output when the following code is executed?public class Product {public static void main(String[] args) { LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01);LocalDate tempLocalDate = startLocalDate;startLocalDate.plusDays(10);System.out.println(tempLocalDate);}} 2016-06-0101-06-20162016-06-1111-06-2016
Solution
The output of the code will be "2016-06-01".
Here's the step by step explanation:
- A
LocalDateobjectstartLocalDateis created with the date 2016-06-01. - A new
LocalDateobjecttempLocalDateis created and assigned the same reference asstartLocalDate. - The
plusDays(10)method is called onstartLocalDate, but the result is not assigned to any variable. TheplusDaysmethod does not change the originalLocalDateobject, but instead returns a newLocalDateobject with the added days. Since the result is not stored, this line effectively does nothing. - The
System.out.println(tempLocalDate);line prints thetempLocalDate, which still has the original date of 2016-06-01.
So, the output will be "2016-06-01".
Similar Questions
Consider the following programimport java.time.*;public class TestParseMethod{ public static void main(String[] args){ String dateString = "2/4/2019"; LocalDate myDate = LocalDate.parse(dateString); System.out.println("You entered " + myDate);}}What will be an output of this program ?Group of answer choicesYou entered 2 Apr 2019It will not compileYou entered 2/4/2019It will create exception
What will be the output of the following program? Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 7); cal.set(Calendar.MONTH, 4); cal.set(Calendar.YEAR, 2015); System.out.println("Week of month " + cal.get(Calendar.WEEK_OF_MONTH)); System.out.println("Day of week in month " + (cal.get(Calendar.DAY_OF_WEEK_IN_MONTH))); System.out.println("Day " + cal.get(Calendar.DAY_OF_MONTH)); System.out.println("Month " + (Calendar.MAY == cal.get(Calendar.MONTH) ? "May" : "April"));
public class TechSol { public static void main(String[] args) { Date startUtilDate = new Date(); // Assume that the obtained date is Thu Nov 03 20:40:45 IST 2016 Date tempUtilDate = startUtilDate; startUtilDate.setDate(26); System.out.println(tempUtilDate); LocalDate startLocalDate = LocalDate.of(2016, Month.JUNE, 01); LocalDate tempLocalDate = startLocalDate; startLocalDate = startLocalDate.plusDays(10); System.out.println(tempLocalDate); }}What will get consoled out during execution?Sat Nov 26 20:40:45 IST 2016 2016-06-01Sat Nov 03 20:40:45 IST 2016 2016-06-01Sat Nov 26 20:40:45 IST 2016 2016-06-10Sat Nov 03 20:40:45 IST 2016 2016-06-10Submit
public class DateTimeSample { public static void main(String[] args) { Period period = Period.of(1, 5, 2001); System.out.println(period.getDays() + "\t" + period.getMonths()); }}What is expected to be consoled out? (Choose any one option)1 55 12001 55 2001
Which of the following will print only the date without time?*System.out.println(new Date());System.out.println(new LocalTime());System.out.println(LocalDate.today());System.out.println(LocalDate.now());
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.