[VV113] The Java 21 Newsletter
????2??1?? Dear followers, let's prepare for Java 21 certification together!
1?? How would you answer this question:
Given:
public static void main( String[] args ) throws IOException {
FileWriter outputStream = null;
try {
outputStream = new FileWriter( "characterOutput.txt" );
outputStream.write( 23 );
} finally {
if ( outputStream != null ) {
outputStream.close();
}
}
}
Choose the right statements. (Choose 2)
A) It will create the file characterOutput.txt if it does not exist.
B) It will append the content if the file characterOutput.txt exists.
C) It will throw an exception if the file characterOutput.txt does not exist.
D) It will throw an exception if characterOutput.txt is a folder.
E) Compilation fails.
#PathToJava21 #java #certification
?? Solution
The correct answers are:
? A) It will create the file characterOutput.txt if it does not exist.
? D) It will throw an exception if characterOutput.txt is a folder.
Explanation:
A) Correct:
The FileWriter(String fileName) constructor will create the file if it does not exist.
B) Incorrect:
By default, FileWriter overwrites the file instead of appending. If appending is needed, the constructor should be new FileWriter("characterOutput.txt", true).
C) Incorrect:
The file will be created if it does not exist, unless there is a permission issue.
D) Correct:
If characterOutput.txt is a folder, FileWriter will throw an IOException, as the documentation states.
E) Incorrect:
The code compiles fine; there are no syntax errors.
Thus, the correct choices are A and D. ??
Character Streams: https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html
(Working with Arrays and Collections)
2?? How would you answer this question:
Given:
LocalDateTime today;
String result;
DateTimeFormatter formatter;
formatter = DateTimeFormatter.ofPattern( "EEE d MMM yy HH:MM");
today = LocalDateTime.of( 2025,3,15,16,26 );
result = formatter.format(today);
System.out.println("Result: " + result);
What is printed?
A) Result: Sat 15 Mar 25 16:26
B) Result: Sat 15 Mar 25 16:03
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
?? Solution
The correct answer is:
B) Result: Sat 15 Mar 25 16:03
Explanation:
The pattern in DateTimeFormatter.ofPattern("EEE d MMM yy HH:MM") contains an issue:
HH → correctly represents hours in a 24-hour format.
MM → incorrectly represents months instead of minutes.
mm → should be used for minutes instead.
Thus, in formatter.format(today), the MM (which should represent minutes) is interpreted as months (March = 03) instead of the correct minutes (26).
This leads to:
Sat 15 Mar 25 16:03
Since there is no compilation error and no runtime exception, options C and D are incorrect.
DateTimeFormatter: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/format/DateTimeFormatter.html
DateTimeFormatter for Time: https://www.baeldung.com/java-datetimeformatter#timeFormatter
(Handling Date, Time, Text, Numeric and Boolean Values)
3?? How would you answer this question:
Given the following are in the same file:
public interface IconicFrenchAnimal {
private static int zero(){
return 0;
}
int pawns(boolean usedToWalk);
}
abstract class Frog implements IconicFrenchAnimal{
}
abstract class Rooster implements IconicFrenchAnimal{
public abstract short pawns(boolean usedToWalk);
}
class Snail implements IconicFrenchAnimal{
int pawns(boolean usedToWalk){
return 0;
}
}
Which types have compilation error(s)?
A) IconicFrenchAnimal
B) Frog
C) Rooster
D) Snail
#PathToJava21 #java #certification
?? Solution
Let's analyze the errors and determine which types have compilation errors.
Key Observations:
* Interface IconicFrenchAnimal
It defines a private static method zero(), which is valid in Java (since Java 9).
It declares the method pawns(boolean usedToWalk);, which is implicitly public abstract.
* Class Frog
It is an abstract class implementing IconicFrenchAnimal.
It does not provide an implementation for pawns(boolean), which is allowed since it's abstract.
* Class Rooster
It declares pawns(boolean) but with a different return type (short instead of int).
This causes a compilation error because it attempts to override the method with an incompatible return type.
* Class Snail
It implements pawns(boolean), but with package-private visibility instead of public.
This causes a compilation error because Java requires interface methods to be implemented with public visibility.
* Errors and Affected Types
Rooster (C): ? Compilation error due to incompatible return type.
Snail (D): ? Compilation error due to reducing method visibility.
* Final Answer
? Correct options: C) Rooster and D) Snail
Interface static methods: https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html#static
(Using Object-Oriented Concepts in Java)
4?? How would you answer this question:
Given:
StringBuilder quoteBuilder = new StringBuilder( "Impossible is not French." );
String quote = new String( quoteBuilder.chars() );
quote = quote.concat( " --Napoleon" );
System.out.println( quote );
What is printed?
A) Impossible is not French.
B) Impossible is not French. --Napoleon
C) --Napoleon
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
?? Solution
The given code has an issue in this line:
String quote = new String( quoteBuilder.chars() );
The problem is that quoteBuilder.chars() returns an IntStream,
but there is no String constructor that accepts an IntStream.
This results in a compilation error.
Breakdown:
1. StringBuilder quoteBuilder = new StringBuilder("Impossible is not French.");
This initializes a StringBuilder with the given string.
2. quoteBuilder.chars() returns an IntStream of Unicode code points of the characters in quoteBuilder.
3. new String( quoteBuilder.chars() );
- This is incorrect because there is no constructor String(IntStream).
- This causes compilation to fail.
Correct Answer:
E) Compilation fails.
StringBuilder: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/StringBuilder.html
(Handling Date, Time, Text, Numeric and Boolean Values)
5?? How would you answer this question:
Given:
public class FrenchPoets {
public static void main( String[] args ) {
String[] frenchPoets = { "Baudelaire", "Rimbaud", "Verlaine" };
setHugoPoet( frenchPoets );
System.out.println( frenchPoets[0] );
}
private static void setHugoPoet( final String[] poets ) {
if ( poets.length > 0 ) {
poets[0] = "Hugo";
}
}
}
What will be printed?
A) Baudelaire
B )Hugo
C) Verlaine
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
?? Solution
The correct answer is:
B) Hugo
Explanation:
The frenchPoets array is initialized with {"Baudelaire", "Rimbaud", "Verlaine"}.
The method setHugoPoet(frenchPoets) is called.
Inside setHugoPoet, the parameter poets is marked as final.
However, this means the reference itself cannot be changed, but the contents of the array can be modified.
Since poets.length > 0, poets[0] is updated from "Baudelaire" to "Hugo".
Back in main, System.out.println(frenchPoets[0]) prints "Hugo".
Thus, the output is Hugo.
(Using Object-Oriented Concepts in Java)
6?? How would you answer this question:
Given:
Locale currentLocale = new Locale("fr", "CA", "UNIX");
ResourceBundle introLabels = ResourceBundle.getBundle(
"ButtonLabel", currentLocale);
And given the following list of ResourceBundle:
Which ResourceBundle will be selected?
A) ButtonLabel_fr_CA_UNIX
B) ButtonLabel_fr
C) ButtonLabel_en_US
D) ButtonLabel_en
E) ButtonLabel
#PathToJava21 #java #certification
?? Solution
The correct answer is:
A) ButtonLabel_fr_CA_UNIX
Explanation:
When calling:
Locale currentLocale = new Locale("fr", "CA", "UNIX");
ResourceBundle introLabels = ResourceBundle.getBundle("ButtonLabel", currentLocale);
The lookup order follows this hierarchy:
ButtonLabel_fr_CA_UNIX (Exact match)
ButtonLabel_fr_CA (Matches language and country but not variant)
ButtonLabel_fr (Matches language only)
ButtonLabel_en_US (Matches default locale, if set to en_US)
ButtonLabel_en (Matches default language, if set to en)
ButtonLabel (Fallback base bundle)
Since ButtonLabel_fr_CA_UNIX is explicitly defined and matches exactly with the requested locale (fr, CA, UNIX), it will be selected.
Thus, the correct answer is A) ButtonLabel_fr_CA_UNIX.
About the ResourceBundle Class: https://docs.oracle.com/javase/tutorial/i18n/resbundle/concept.html
ResourceBundle: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/ResourceBundle.html
(Implementing Localization)