🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!
1️⃣ How would you answer this question:
Which of the following is not a module directive in the Java Platform Module System?
A) provides
B) uses
C) opens
D) exports
E) requires
F) depends
#PathToJava21 #java #certification
💡 Solution
The correct answer is:
F) depends
In the Java Platform Module System (JPMS), the following are valid module directives:
requires – Specifies a dependency on another module.
exports – Makes a package accessible to other modules.
opens – Makes a package accessible for deep reflection (e.g., with reflection APIs).
uses – Specifies a service interface that the module consumes.
provides – Specifies a service implementation for a service interface.
However, depends is not a valid module directive in Java.
[JLS] Module Declarations : https://docs.oracle.com/javase/specs/jls/se21/html/jls-7.html#jls-7.7
(Packaging and Deploying Java Code)
2️⃣ How would you answer this question:
Given:
and monuments.txt file is in the right location,
and monuments.txt file contains:
Eiffel Tower
Arc de Triomphe
Notre-Dame de Paris
Palace of Versailles
Mont Saint-Michel
A) It prints the content of monuments.txt
B) It runs successfully without printing anything.
C) It throws an exception.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
The provided code contains an issue in the try block:
try ( reader = new BufferedReader(new FileReader(filePath)) ) {
This is incorrect because in a try-with-resources statement, the resource must be declared inside the parentheses.
The correct syntax would be:
try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
Since the code does not follow this rule, it fails to compile.
Correct Answer:
D) Compilation fails.
LocalVariableDeclaration: https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-LocalVariableDeclaration
The try-with-resources Statement: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
(Handling Exceptions)
3️⃣ How would you answer this question:
Given:
Which of the following suggestions prints :
"France international dialing code is in the list: 33" ? (Choose 2)
A) UnaryOperator onlyFrance = i -> 33;
B) IntPredicate onlyFrance = i -> i == 33;
C) Function onlyFrance = i -> i == 33;
D) Predicate onlyFrance = i -> i == 33;
#PathToJava21 #java #certification
💡 Solution
Let's analyze each option carefully and determine which two will correctly print:
"France international dialing code is in the list: 33"
Breakdown:
The forEach method iterates over the phoneCountryCode list.
onlyFrance.test(i) must return true for i == 33 to print the desired output.
The onlyFrance variable must be of a type that has a test method.
Evaluating the Options:
A) UnaryOperator onlyFrance = i -> 33;
🚫 Incorrect
UnaryOperator is a functional interface for a function that takes an argument and returns a value of the same type.
The expression i -> 33 always returns 33, regardless of i, meaning test(i) doesn’t exist.
Compilation error (UnaryOperator does not have test).
B) IntPredicate onlyFrance = i -> i == 33;
✅ Correct
IntPredicate is a functional interface specifically for primitive int values.
test(int value) method exists.
i == 33 will return true only for i = 33, leading to the expected output.
C) Function onlyFrance = i -> i == 33;
🚫 Incorrect
Function is used for transforming a value of type T into a return value of type R.
The function returns a Boolean, but Function does not have a test method.
Compilation error (Function does not have test).
D) Predicate onlyFrance = i -> i == 33;
✅ Correct
Predicate is a functional interface that defines test(T t), which returns a boolean.
i -> i == 33 properly checks if i is 33 and returns true for 33.
Correctly prints the expected message.
Final Answer:
✅ B) IntPredicate onlyFrance = i -> i == 33;
✅ D) Predicate onlyFrance = i -> i == 33;
(Working with Streams and Lambda expressions)
4️⃣ How would you answer this question:
In Java Platform Module System, which of the following statements are true? (Choose 2)
A) At compilation, the module-info.java becomes a ".properties" file
B) module-info.java file can have only one module name
C) module-info.java file must not be empty
D) module-info.java file can be located inside any folder
#PathToJava21 #java #certification
💡 Solution
The correct answers are:
B) module-info.java file can have only one module name
C) module-info.java file must not be empty
Explanation:
A) At compilation, the module-info.java becomes a ".properties" file ❌
Incorrect. When compiled, module-info.java is transformed into module-info.class, not a .properties file.
B) module-info.java file can have only one module name ✅
Correct. A module-info.java file defines exactly one module. You cannot declare multiple modules in a single file.
C) module-info.java file must not be empty ✅
Correct. The module-info.java file must contain at least the module moduleName {} declaration. An empty file would cause a compilation error.
D) module-info.java file can be located inside any folder ❌
Incorrect. The module-info.java file must be placed at the root of the src// directory to be recognized by the Java compiler.
Understanding Java 9 Modules : https://www.oracle.com/corporate/features/understanding-java-9-modules.html
(Packaging and Deploying Java Code)
5️⃣ How would you answer this question:
Given:
What happens?
A) It prints: "Estimated population in 2030: 68.9 million"
B) It prints: "Estimated population in 2030: 69 million"
C) It throws an exception at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
The correct answer is D) Compilation fails.
Explanation:
The error occurs because you're trying to assign the result of frenchPopulation * Math.exp(0.0025 * 5) to a long variable,
frenchPopulationIn2020, but Math.exp() returns a double.
Java does not allow implicit conversion from double to long due to the potential for loss of precision.
You will get a compilation error like this:
java: incompatible types: possible lossy conversion from double to long
How to fix:
You can either:
Change frenchPopulationIn2020 to double:
double frenchPopulationIn2020 = frenchPopulation * Math.exp(0.0025 * 5);
Or explicitly cast the result to long:
long frenchPopulationIn2020 = (long) (frenchPopulation * Math.exp(0.0025 * 5));
Once you make the change, the code will compile and run correctly.
[JLS] 5.1.2. Widening Primitive Conversion: https://docs.oracle.com/javase/specs/jls/se21/html/jls-5.html#jls-5.1.2
19 specific conversions on primitive types are called the widening primitive conversions:
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
(Handling Date, Time, Text, Numeric and Boolean Values)
6️⃣ How would you answer this question:
Given:
What is printed?
A) 0 0
B) -1 0
C) 1 0
D) It throws an exception at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
🔎 More: https://lnkd.in/eZKYX5hP
💡 Solution
Understanding Arrays.compare()
Arrays.compare(arrayEven, arrayOdd) performs a lexicographical comparison:
It compares elements pairwise until a difference is found.
If all elements are equal, it returns 0.
If the first differing element in arrayEven is smaller, it returns -1.
If the first differing element in arrayEven is greater, it returns 1.
Step-by-step comparison:
0 (from arrayEven) vs 1 (from arrayOdd) → 0 < 1
Since 0 is less than 1, Arrays.compare() immediately returns -1.
Understanding Arrays.mismatch()
Arrays.mismatch(arrayEven, arrayOdd) finds the first index where elements differ.
If arrays are identical, it returns -1.
Otherwise, it returns the index of the first mismatching element.
Step-by-step mismatch check:
Index 0: 0 vs 1 (Mismatch found)
So, Arrays.mismatch() returns 0.
Final Output:
System.out.println(compareResult + " " + mismatchResult);
compareResult = -1
mismatchResult = 0
The printed output is:
-1 0
Correct Answer:
B) -1 0
Arrays: https://lnkd.in/eaGTvN-p
(Working with Arrays and Collections)