Java OCA OCP Practice Question 1399

Question

How many lines contain a compile error?

1:   import java.time.*; 
2:   import java.time.format.*; 
3: /*w w w  .  j a va 2 s  . c om*/
4:   public class Main { 
5:      public void main(String h) { 
6:         LocalDate newYears = new LocalDate(2020, 1, 1); 
7:         Period period = Period.ofYears(1).ofDays(1); 
8:         DateTimeFormat format = DateTimeFormat.ofPattern("MM-dd-yyyy"); 
9:         System.out.print(format.format(newYears.minus(period))); 
10:    } 
11:  } 
  • A. None
  • B. One
  • C. Two
  • D. Three
  • E. Four
  • F. Five


C.

Note

Line 5 does not declare a main() method that can be the entry point to the program.

It does correctly declare a regular instance method and does compile.

Line 6 does not compile because LocalDate needs to use a static method rather than a constructor.

Line 7 is incorrect because Period methods should not be chained.

However, it does compile, returning a period of 1 day.

Line 8 does not compile because the correct class name is DateTimeFormatter.

Line 9 is correct.

Option C is correct because lines 6 and 8 do not compile.




PreviousNext

Related