Java Formatting, Dates, and Localization Flashcards
6 cards from real SCJP practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 6 Java Formatting, Dates, and Localization flashcards as text
Which class should be used to format a number as currency for a specific locale in Java?
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.
What is the output of `System.out.printf("%05.1f%n", 3.5);`?
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.
Which statement about `java.util.Calendar` is TRUE in Java SE 6?
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.
Which of the following correctly creates a Locale for French as spoken in France?
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.
What does `DateFormat.getDateInstance(DateFormat.SHORT, locale)` return?
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.
What is the output of the following code? String s = String.format("%-10s|", "hi");
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 |'.
How this page was made: written with AI tools and published by Nathan Beja, founder of Practice Test Geeks, who is responsible for its content. We have not yet matched this exam's facts to an official source, so confirm fees, dates and rules with the exam body. Not reviewed by a subject-matter expert. Editorial policy