Java - Things to know before your OCJP Exam - Utility Classes

1. * DateFormat is an abstract class. It provides a getDateInstance() static method to get Date Instance.

2. Following are valid Locale constructor

Locale ( String language )
Locale ( String language, String country )
Locale ( String language , String country, String variant )

3. While doing formatting using %f for float, it rounds the float to the closest value if less decimals are required. For ex - 3.97 becomes 3.4 if we do %.1f

4. * NumberFormat.setMaximumFractionDigits() method sets the max no of digits allowed in fraction portion of number.

5. Calendar is an abstract class and hence concrete subclass object should be instantiated using Calendar.getInstance(). Usually it returns the object of GeorgianCalendar.

6. Following DateFormat initialization are correct.

DateFormat df = DateFormat.getInstance();

DateFormat df = DateFormat.getDateInstance();
DateFormat df = DateFormat.getDateInstance(DateFormat.FULL);

8. Out of the following code , only 3rd is correct

Date date = DateFormat.newInstance(DateFormat.LONG, Locale.US).parse(str); 

Date date = DateFormat.newInstance(DateFormat.LONG, Locale.US).format(str); 

Date date = DateFormat.getDateInstance(DateFormat.LONG, Locale.US).parse(str);

9. split() and macthes() methods are there in Pattern as well as String class.