SCJP Java Formatting, Dates, and Localization โ Questions and Answers
Question 1: Which class should be used to format a number as currency for a specific locale in Java?
- java.util.Currency
- java.text.DecimalFormat using pattern 'ยค#,##0.00'
- java.text.NumberFormat obtained via NumberFormat.getCurrencyInstance(locale) (Correct answer)
- java.lang.String.format() with the %c specifier
Correct answer: java.text.NumberFormat obtained via NumberFormat.getCurrencyInstance(locale)
`NumberFormat.getCurrencyInstance(Locale)` returns a NumberFormat preconfigured for the currency of the given locale. While DecimalFormat can approximate it, the standard SCJP answer is `NumberFormat.getCurrencyInstance()`. java.util.Currency represents a currency but doesn't format numbers.
Question 2: What is the output of `System.out.printf("%05.1f%n", 3.5);`?
- 3.5
- 003.5 (Correct answer)
- 3.5
- 0003.5
Correct answer: 003.5
The format `%05.1f` means: width 5, zero-padded, 1 decimal place. '3.5' formatted to 1 decimal is '3.5' (3 chars). To reach width 5, two zeros are prepended: '003.5'. The `%n` emits a platform-specific newline.
Question 3: Which statement about `java.util.Calendar` is TRUE in Java SE 6?
- Calendar.MONTH is zero-based (January = 0) (Correct answer)
- Calendar.DAY_OF_MONTH is zero-based (first day = 0)
- Calendar instances are immutable
- You instantiate Calendar directly with new Calendar()
Correct answer: Calendar.MONTH is zero-based (January = 0)
Calendar.MONTH is notoriously zero-based: January = 0, February = 1, โฆ December = 11. DAY_OF_MONTH is 1-based. Calendar is mutable, and you cannot instantiate it directly โ you use Calendar.getInstance() because the constructor is protected.
Question 4: Which of the following correctly creates a Locale for French as spoken in France?
- new Locale("France", "French")
- new Locale("fr", "FR") (Correct answer)
- Locale.getFranceInstance()
- new Locale("French", "FR")
Correct answer: new Locale("fr", "FR")
The Locale(String language, String country) constructor takes ISO 639-1 language code ('fr') and ISO 3166-1 country code ('FR'). Language codes are lowercase, country codes are uppercase. 'France' and 'French' are display names, not codes.
Question 5: What does `DateFormat.getDateInstance(DateFormat.SHORT, locale)` return?
- A DateFormat that formats dates and times
- A DateFormat that formats only dates in a short locale-specific style (Correct answer)
- A DateFormat that always returns dates in MM/dd/yy format regardless of locale
- A SimpleDateFormat using the pattern 'M/d/yy'
Correct answer: A DateFormat that formats only dates in a short locale-specific style
`DateFormat.getDateInstance(style, locale)` returns a date-only formatter (no time portion) styled appropriately for the locale. SHORT typically yields something like '7/30/26' in the US, but the actual format depends on the locale โ it is locale-sensitive, not hard-coded.
Question 6: What is the output of the following code? String s = String.format("%-10s|", "hi");
- "|hi |"
- "hi |" (Correct answer)
- " hi|"
- "hi| "
Correct answer: "hi |"
The `%-10s` format specifier means: string, width 10, left-aligned (the `-` flag). 'hi' is 2 characters, so 8 spaces are appended to reach width 10, followed by '|'. Result: 'hi |'.
Which class should be used to format a number as currency for a specific locale in Java?