🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!
1️⃣ How would you answer this question:
Given:
and that the orders.csv contains:
What is printed?
A)
1,Kylian Mbappé,Keyboard,2,25.50
2,Teddy Riner,Mouse,1,15.99
3,Sebastien Loeb,Monitor,1,199.99
4,Antoine Griezmann,Headset,3,45.00
B)
1,Kylian Mbappé,Keyboard,2,25.50
2,Teddy Riner,Mouse,1,15.99
C)
2,Teddy Riner,Mouse,1,15.99
3,Sebastien Loeb,Monitor,1,199.99
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Files.readAllLines() returns a List, not a Stream.
This means that the code would not compile as is because List does not have the skip() and limit() methods available, which are part of the Stream API.
So, the correct answer is E) Compilation fails.
Processing Data with Java SE 8 Streams, Part 1: https://www.oracle.com/technical-resources/articles/java/ma14-java-se-8-streams.html
(Using Java I/O API)
2️⃣ How would you answer this question:
Which of the following statements of local Variables Declared With var are invalid? (Choose 5)
A) var a = 1;
B) var b = 2, c = 3.0;
C) var d[] = new int[4];
D) var e;
E) var f = { 6 };
F) var g = (g = 7);
G) var h = (g = 7);
#PathToJava21 #java #certification
💡 Solution
Let's analyze each statement carefully. The var keyword in Java allows local variable type inference, meaning the type is inferred from the initializer. However, there are certain restrictions:
A) var a = 1; ✅ Valid
The type is inferred as int, so this is a valid statement.
B) var b = 2, c = 3.0; ❌ Invalid
Multiple variables cannot be declared in a single statement with var.
C) var d[] = new int[4]; ❌ Invalid
Array declarations using var must be written as var d = new int[4];. The use of var d[] is not allowed.
D) var e; ❌ Invalid
var requires an explicit initializer to infer the type.
E) var f = { 6 }; ❌ Invalid
This is an array initializer, which requires an explicit type (int[] f = {6};). The compiler cannot infer the type from {6} alone.
F) var g = (g = 7); ❌ Invalid
g is used before being declared, leading to a compilation error.
G) var h = (g = 7); ✅ Valid
g = 7 results in an int, so h is inferred as int.
Final Answer: The 5 invalid statements are:
B, C, D, E, and F.
[JLS] 14.4. Local Variable Declaration Statements: https://docs.oracle.com/javase/specs/jls/se14/html/jls-14.html#jls-14.4
(Using Object-Oriented Concepts in Java)
3️⃣ How would you answer this question:
Given:
What is printed?
- A) bim boom bam
- B) bim bam boom
- C) bim boom
- D) 'bim bam ' followed by an exception
- E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Step-by-Step Execution:
1. Entering try block
"bim " is printed.
An Exception is thrown.
2. Before handling the exception, close() is called
The try-with-resources mechanism ensures that the close() method is called before the catch block executes.
Inside close(), "bam " is printed.
The close() method throws a RuntimeException, but this does not replace the original Exception from the try block. Instead, it becomes a suppressed exception.
3. Handling the Exception in catch
The catch (Exception e) block only catches the original Exception thrown inside try.
"boom " is printed.
4. What happens to the RuntimeException from close()?
It is suppressed and does not propagate.
The JVM allows the catch (Exception e) block to complete execution normally without rethrowing suppressed exceptions.
No exception is printed or thrown at runtime.
Final Printed Output:
bim bam boom
✔ No RuntimeException is thrown or printed.
Correct Answer:
✅ B) bim bam boom
The try-with-resources Statement: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
An exception can be thrown from the block of code associated with the try-with-resources statement.
In the class BoomBoom, an exception can be thrown from the try block,
and up to two exceptions can be thrown from the try-with-resources statement when it tries to close the BoomBoom object.
If an exception 'new Exception()' is thrown from the try block and one (or more) exception(s) 'new RuntimeException()'
is thrown from the try-with-resources statement,
then this exception 'new RuntimeException()' thrown from the try-with-resources statement is suppressed,
and the exception thrown 'new Exception()' by the block is the one that is thrown by the BoomBoom main method.
You can retrieve these suppressed exceptions by calling the Throwable.getSuppressed method from the exception thrown by the try block.
(Handling Exceptions)
4️⃣ How would you answer this question:
Given:
What is printed?
A) true
B) false
C) A NullPointerException is thrown.
D) A ClassCastException is thrown.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Let's analyze the code step by step to determine the output.
Code Breakdown:
Key Points:
o1.equals(o3) is equivalent to "1789".equals("1789"), since:
o1 is a String with "1789".
o3 is also a String with "1789" (converted via Integer.toString()).
The String.equals() method compares values, and since both strings are "1789", it returns true.
Final Output:
✅ A) true
(Using Object-Oriented Concepts in Java)
5️⃣ How would you answer this question:
Given:
What is printed?
- A) {2=Crazy Horse, 3=Paradis Latin, 4=Le Lido, 5=Folies Bergère}
- B) {2=Crazy Horse, 3=Paradis Latin, 4=Le Lido}
- C) {}
- D) An exception is thrown at runtime.
- E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Step-by-Step Analysis:
1. Understanding TreeMap.subMap(fromKey, fromInclusive, toKey, toInclusive)
The subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) method returns a view of a portion of the map.
It includes entries starting from fromKey (if fromInclusive is true).
It includes entries up to toKey (if toInclusive is true).
2. Extracting the Relevant Entries
The TreeMap is sorted in ascending order:
{1=Moulin Rouge, 2=Crazy Horse, 3=Paradis Latin, 4=Le Lido, 5=Folies Bergère}
Now, applying:
cabarets.subMap(2, true, 5, false)
Start from key 2, including it (true).
Stop before key 5, excluding it (false).
This includes the keys 2, 3, and 4.
The resulting subMap:
{2=Crazy Horse, 3=Paradis Latin, 4=Le Lido}
Final Answer:
✅ B) {2=Crazy Horse, 3=Paradis Latin, 4=Le Lido}
(Working with Arrays and Collections)
6️⃣ Which of the following suggestions compile? Choose two.
A)
B)
C)
D)
#PathToJava21 #java #certification
💡 Solution
Understanding Sealed Classes in Java
A sealed class restricts which classes can extend it.
Any direct subclass of a sealed class must be explicitly declared as final, sealed, or non-sealed.
Now, let's analyze each option:
- Option A ✅ (Valid)
Figure is sealed and explicitly permits Rectangle.
Rectangle is final, which is allowed because a subclass of a sealed class can be:
. final (no further subclassing)
. sealed (continues restriction)
. non-sealed (allows open inheritance)
✅ Correct usage of sealed classes!
- Option B ❌ (Invalid)
This produced the error message "Modifier 'sealed', 'non-sealed' or 'final' expected" that tells us that Rectangle must declare one of these modifiers.
Since Figure is sealed, all direct subclasses must specify whether they are final, sealed, or non-sealed.
Fix: Add one of the required modifiers:
final class Rectangle extends Figure {} // ✅ Valid
OR
non-sealed class Rectangle extends Figure {} // ✅ Also valid
- Option C ❌ (Invalid)
final sealed is not allowed because:
final means no subclassing.
sealed means controlled subclassing.
These two contradict each other!
Fix: Remove either final or sealed:
sealed class Circle extends Figure {} // ✅ Valid if you want controlled subclassing
OR
final class Circle extends Figure {} // ✅ Valid if you want no further subclassing
- Option D ✅ (Valid)
Figure is sealed and permits Circle and Rectangle.
Circle is final, meaning it cannot be subclassed further.
Rectangle is non-sealed, meaning it can be freely extended by other classes.
✅ This is a perfect example of using sealed classes correctly!
- Final Verdict
Option Valid? Why?
A ✅ Correct use of sealed and final.
B ❌ Missing final, sealed, or non-sealed in Rectangle.
C ❌ final sealed is not allowed.
D ✅ Correct combination of sealed, final, and non-sealed.
- Key Takeaways
Sealed classes control which classes can extend them.
Direct subclasses of a sealed class must be final, sealed, or non-sealed.
final means no further subclassing, sealed means controlled subclassing, and non-sealed means open subclassing.
(Using Object-Oriented Concepts in Java)