Java OCA OCP Practice Question 2532

Question

Which of the following code options accesses the default locale for the host system and creates a new locale with the default locale's language and country?

a  Locale locale = Locale.getDefaultLocale();
   Locale newLocale = new Locale(locale.getLanguage(), locale.getCountry());

b  Locale locale = Locale.getDefault();
   Locale newLocale = new Locale(locale.getLanguage(), locale.getCountry());

c  Locale locale = Locale.getDefault();
   Locale newLocale = new Locale(locale.getDisplayLanguage(),
   locale.getDisplayCountry());//from w  w w  . j a  v a  2 s .  co m

d  Locale locale = Locale.getDefaultLocale();
   Locale newLocale = new Locale(locale);


b

Note

Options (a) and (d) are incorrect because they use nonexistent method names to access the default locale.

The correct factory method name is Locale.getDefault().

Option (c) is incorrect because the Locale's constructor should be passed language and country codes and not their display names.

The methods getDisplayLanguage() and getDisplayCountry() retrieve display names for languages and countries.

The correct methods to use here are getLanguage() and getCountry(), which return codes for the language and country.




PreviousNext

Related