Which code fragment, inserted at line 23, allows the code to compile?

Given:

18. import java.util.Date;
19. import java.text.DateFormat;
20.
21. DateFormat df;
22. Date date = new Date();
23. //insert code here
24. String s = df.format(date);

Which code fragment, inserted at line 23, allows the code to compile?

Given:

18. import java.util.Date;
19. import java.text.DateFormat;
20.
21. DateFormat df;
22. Date date = new Date();
23. //insert code here
24. String s = df.format(date);

Which code fragment, inserted at line 23, allows the code to compile?

A.
df = new DateFormat();

B.
df = Date.getFormat();

C.
df = date.getFormat();

D.
df = DateFormat.getFormat();

E.
df = DateFormat.getInstance();

Explanation:
A:
Main.java:23: java.text.DateFormat is abstract; cannot be instantiated
df = new DateFormat();
^
1 error

B:
Main.java:23: cannot find symbol
symbol : method getFormat()
location: class java.util.Date
df = Date.getFormat();
^
1 error

C:
Main.java:23: cannot find symbol
symbol : method getFormat()
location: class java.util.Date
df = date.getFormat();
^
1 error

D:
Main.java:23: cannot find symbol
symbol : method getFormat()
location: class java.text.DateFormat
df = DateFormat.getFormat();
^
1 error

E:
compiled successfully



Leave a Reply 0

Your email address will not be published. Required fields are marked *