Localization practice questions

From OCP Java SE 8 (1Z0-809) · 16 questions on this topic

Localization practice questions from OCP Java SE 8 (1Z0-809). This pack has 16 questions tagged Localization, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Localization

  1. Question 1

    What is the output of the following program? ```java import java.text.NumberFormat; import java.util.Locale; public class Main { public static void main(String[] args) throws Exception { NumberFormat nf = NumberFormat.getInstance(Locale.US); System.out.println(nf.parse("40.45abc")); } } ```

    1. A. 40.45Correct answer

      parse consumes as many characters as form a valid number and stops at the first invalid one, so it reads 40.45 and silently ignores the trailing "abc".

    2. B. 40

      parse does not truncate to a whole number; it reads the entire valid numeric prefix including the decimal portion before it stops, yielding 40.45 rather than 40.

    3. C. Compilation fails because parse does not throw a checked exception

      parse does declare the checked ParseException, which is exactly why main carries the throws clause; the code compiles cleanly.

    4. D. A ParseException is thrown because of the trailing letters

      Trailing garbage after a valid number does not trigger an exception; ParseException fires only when the string does not even begin with a parseable number.

    Explanation

    NumberFormat.parse reads as far as it can and stops at the first character that cannot extend the number, so a valid numeric prefix is returned and any trailing text is simply ignored. An exception is raised only when the input does not begin with a parseable number at all. Because parse declares the checked ParseException, main must declare or handle it, and the program compiles and runs.

  2. Question 2

    What is the output of the following program? ```java import java.util.Locale; public class Main { public static void main(String[] args) { Locale l = new Locale.Builder() .setLanguage("de") .setRegion("CH") .build(); System.out.println(l); } } ```

    1. A. de-CH

      The hyphen form is what toLanguageTag produces; toString uses an underscore.

    2. B. Compilation fails because Builder requires a country before a language

      Builder setters have no required ordering.

    3. C. de_CHCorrect answer

      Correct: toString joins language and country with an underscore.

    4. D. CH_de

      The order is language then country, not reversed.

    Explanation

    Locale.toString joins language and country with an underscore. Builder setters may be invoked in any order, and the hyphenated form belongs to the IETF language tag rather than toString.

  3. Question 3

    Which Locale construction follows the language/country conventions correctly?

    1. A. new Locale("en", "US")Correct answer

      Follows the convention exactly: lowercase language code first, uppercase country code second.

    2. B. new Locale("EN", "us")

      The casing is reversed. The constructor does not validate or fix casing, so this builds a real but wrong locale that silently matches nothing — a notorious quiet bug.

    3. C. new Locale("US", "en")

      The language and country arguments are swapped. Again the constructor accepts it without complaint, producing a valid-looking locale that resolves to nothing intended.

    4. D. new Locale("en_US")

      This crams both codes into the single language slot; the underscore string is treated as one language subtag, not as a language plus a country.

    Explanation

    The two-argument Locale constructor expects a lowercase language code first and an uppercase country code second. It performs no validation and does not correct casing or argument order, so a mis-cased, swapped, or combined string still constructs successfully but points at the wrong (or a meaningless) locale. Getting the conventional form right is what guarantees the locale resolves to the intended language and country.

  4. Question 4

    A Properties object has no entry for the key "timeout". What do p.get("timeout") and p.getProperty("timeout", "30") return, respectively?

    1. A. An exception and "30"

      A missing key does not raise an exception from get; the inherited Hashtable get simply returns null for an absent key rather than throwing.

    2. B. null and "30"Correct answer

      The plain Hashtable get returns null for a missing key, while the two-argument getProperty falls back to and returns its supplied default "30".

    3. C. "30" and "30"

      This wrongly assumes get also honors a default; get is the inherited Hashtable method with no default support, so it returns null, not "30".

    4. D. null and null

      This ignores the default argument passed to getProperty; the two-argument form returns the supplied "30" when the key is absent rather than null.

    Explanation

    get is the plain Hashtable method — null for a missing key, no default support. getProperty's two-argument form returns the supplied default "30". Only getProperty understands defaults; mixing the two up silently loses fallback values.

  5. Question 5

    What is the output of the following program? ```java import java.text.NumberFormat; import java.util.Locale; public class Main { public static void main(String[] args) { System.out.println(NumberFormat.getCurrencyInstance(Locale.US).format(2.5)); } } ```

    1. A. $2.5

      The fraction digits are dictated by the locale's currency, not by the raw double's precision, so the value is padded to two decimal places rather than left as a single digit.

    2. B. 2.5

      A currency formatter prepends the locale's currency symbol and applies its fraction-digit rule, so the output is not a bare number.

    3. C. USD 2.5

      The US locale renders currency with the "$" symbol, not the ISO code "USD", and still pads to two fraction digits.

    4. D. $2.50Correct answer

      The US currency formatter prefixes the dollar sign and pads to the currency's two fraction digits, producing $2.50.

    Explanation

    A currency formatter renders values according to its locale's currency rules, not the incoming double's precision. For the US locale that means the dollar-sign prefix and exactly two fraction digits, so a value of 2.5 is padded and printed as $2.50 rather than left with a single decimal.

  6. Question 6

    ResourceBundle.getBundle("Zoo", new Locale("en", "US")) is called and the default locale is fr_FR. Which candidate bundle is checked FIRST?

    1. A. Zoo

      The base name is checked last, after all locale-specific candidates fail.

    2. B. Zoo_en

      The language-only name is tried after the language-plus-country name, not first.

    3. C. Zoo_en_USCorrect answer

      Correct: the most specific requested-locale candidate, language and country combined, is checked first.

    4. D. Zoo_fr_FR

      The default locale's chain is consulted only after the requested locale's candidates fail.

    Explanation

    Bundle lookup begins with the most specific candidate for the requested locale, combining language and country, before widening. The requested locale's chain is tried before the default locale's and before the bare base name.

  7. Question 7

    What is the output of the following program? ```java import java.text.NumberFormat; import java.util.Locale; public class Main { public static void main(String[] args) { NumberFormat nf = NumberFormat.getInstance(Locale.US); System.out.println(nf.format(1234567)); } } ```

    1. A. 1234567

      The general instance adds grouping separators rather than printing bare digits.

    2. B. 1,234,567Correct answer

      Correct: US grouping inserts commas between thousands.

    3. C. 1.234.567

      Dot grouping is a European-style convention, not the US format.

    4. D. $1,234,567.00

      The currency symbol and fixed decimals come from the currency instance, not the general one.

    Explanation

    The general US number format inserts comma grouping separators between thousands. Currency symbols and forced decimals belong to the currency instance, and dot grouping is a different locale's convention.

  8. Question 8

    What is the output of the following program? ```java import java.util.Locale; public class Main { public static void main(String[] args) { Locale l = new Locale("fr", "CA"); System.out.println(l); } } ```

    1. A. fr_CACorrect answer

      Locale.toString emits the language code in lowercase, an underscore, then the country code in uppercase: fr_CA.

    2. B. FR_ca

      This inverts the casing convention (uppercase language, lowercase country); toString normalizes to lowercase language and uppercase country regardless of how the arguments were typed.

    3. C. French (Canada)

      This is the human-readable display name produced by getDisplayName(), not the raw code form that toString returns.

    4. D. fr-CA

      The hyphenated form is the IETF BCP 47 language tag from toLanguageTag(); toString uses an underscore, not a hyphen.

    Explanation

    Locale.toString formats a locale as its lowercase language code, an underscore separator, and its uppercase country code. The casing is normalized by the API, so it does not depend on how the constructor arguments were cased. Other renderings such as the hyphenated language tag or the localized display name come from different methods entirely.

Practise all 16 Localization questions

OCP Java SE 8 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCP Java SE 8

Other topics in this pack