Java OCA OCP Practice Question 1430

Question

What is the output of the following?

14:  int count = 0; 
15:  LocalDate date = LocalDate.of(2020, Month.JANUARY, 1); 
16:  while (date.getMonth() != Month.APRIL) 
17:     date = date.minusMonths(1); 
18:     count++; 
19:  System.out.println(count); 
  • A. 0
  • B. 1
  • C. 3
  • D. 9
  • E. This is an infinite loop.
  • F. The code does not compile.


B.

Note

Since there are not brackets around the while loop, only line 17 is in the loop body.

Line 18 gets executed once after the loop completes.

count will be 1 assuming the loop completes.

Subtracting a month from JANUARY results in DECEMBER.

Since the loop completes E is incorrect and Option B is the answer.

If the brackets were added as the indentation suggests, Option D would be the answer since we are counting months backwards.




PreviousNext

Related