🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!
QUESTION 1️⃣
How would you answer this question:
Given:
What is printed?
A) -10
B) Integer.MIN_VALUE
C) 1
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Let's break it down:
What's happening:
The identity in the reduce call is Integer.MIN_VALUE, which is -2147483648.
The binary operator (i, j) -> i < j ? i : j keeps the minimum of the two numbers.
Step-by-step reduction:
Start with identity: -2147483648
Apply with -10: min(-2147483648, -10) = -2147483648
Apply with 1: min(-2147483648, 1) = -2147483648
Apply with 10: min(-2147483648, 10) = -2147483648
Apply with 100: min(-2147483648, 100) = -2147483648
Final result: -2147483648
Therefore, the printed value is:
B) Integer.MIN_VALUE
(Working with Streams and Lambda expressions)
LESSON 1️⃣
🎭 Java's Pattern Matching with switch + when – at a Paris Cabaret 🎩
Imagine you're managing performers at a Paris cabaret
– from the mysterious “Z” with a one-letter stage name to glamorous stars like “Chantalle Étoile.”
Now, what if Java could help you sort them elegantly?
➡️ That’s where pattern matching in switch with when clauses comes into play!
🔍 Classic Java:
Until recently, handling logic like this required some boilerplate:
Functional? Yes.
Elegant? Not so much.
✨ Modern Java (with style):
With pattern matching and when, you get clarity and expressiveness:
➡️ The when clause is a guard – a Boolean condition that refines the match.
The case only applies if:
1️⃣ The pattern (String name) matches, and
2️⃣ The condition (name.length() == 1) is true.
So “Z” is printed as a Short name, while “Chantalle Étoile” gets the full Performer treatment.
📚 Syntax recap:
case when -> action;
This combo makes Java switch smarter, safer, and more expressive — a real upgrade when you're modeling real-world logic with complex data structures.
#Java #PatternMatching #CodeBetter #JDK21 #JavaTips #CleanCode #SoftwareEngineering
QUESTION 2️⃣
How would you answer this question:
Given:
What is printed?
A) false, false, false
B) true, false, false
C) true, false, true
D) true, true, true
#PathToJava21 #java #certification
💡 Solution
✅ Step-by-step evaluation:
1. (croissantList.getClass() == painAuChocolatList.getClass())
Both are created using new ArrayList<>(), so their runtime class is java.util.ArrayList.
✅ This is true.
2. (croissantList == painAuChocolatList)
== compares object references.
These are two separate instances, so the references are not the same.
✅ This is false.
3. (croissantList.equals(painAuChocolatList))
.equals() in ArrayList compares contents.
Both lists are empty, so they are considered equal.
✅ This is true.
✅ Final Output:
true, false, true
✔️ Correct answer: C) true, false, true
(Using Object-Oriented Concepts in Java)
QUESTION 3️⃣
How would you answer this question:
How to instantiate a MonthDay class? Select 2.
A) var bastilleDay = new MonthDay(7, 14);
B) var bastilleDay = new MonthDay(14, 7);
C) var bastilleDay = MonthDay.of(7, 14);
D) var bastilleDay = MonthDay.of(14, 7);
E) var bastilleDay = MonthDay.of(Month.JULY, 14);
#PathToJava21 #java #certification
💡 Solution
To instantiate a MonthDay class in Java, you should use the static factory method MonthDay.of(...). This method accepts either:
two integers representing the month and the day of the month, or
a Month enum and an integer for the day of the month.
The correct ways to instantiate a MonthDay are:
var bastilleDay = MonthDay.of(7, 14);
var bastilleDay = MonthDay.of(Month.JULY, 14);
Invalid examples include using the new keyword (since MonthDay has no public constructor) or passing invalid month values like 14.
MonthDay: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/time/MonthDay.html
(Handling Date, Time, Text, Numeric and Boolean Values)
LESSON 2️⃣
🚀 Mastering Java Pattern Matching: Understanding Label Dominance 🇫🇷
Java's enhanced switch with pattern matching is a powerful feature that can simplify your code.
However, understanding label dominance is key to writing predictable, error-free code.
Let's explore this concept using Parisian monuments for a fun twist! 🗼
What is Pattern Label Dominance?
In a switch block, multiple pattern labels can match the same expression.
However, one pattern may dominate others, causing the later ones to be unreachable.
This behavior ensures predictability.
Common Pitfalls with Label Dominance
Dominating General Patterns If a general pattern (e.g., Monument) comes before a more specific one (e.g., EiffelTower),
the general pattern will always match first, making the specific one unreachable.
Note: Guarded Patterns Don’t Dominate Constants Guarded patterns, like those using when, do not dominate constant labels.
Guarded patterns are evaluated dynamically, so they don’t prevent constant matches.
Best Practices for Label Order
To avoid issues, order case labels properly:
- Constant labels first (e.g., case 300)
- Guarded patterns second (e.g., case Integer h when h > 100)
- Generic patterns last (e.g., case Integer h)
Example of Best Practice
Let’s create cleaner Java code — just like the elegance of Parisian monuments! 🗼✨
#Java #PatternMatching #SwitchStatement #CodeQuality #CleanCode #SoftwareEngineering
QUESTION 4️⃣
How would you answer this question:
Given:
What will be printed?
A) The list of overseas regions in a random ordered.
B) Always: "Guadeloupe, Martinique, Guyane, La Réunion, Mayotte, "
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
The correct answer is:
B) Always: "Guadeloupe, Martinique, Guyane, La Réunion, Mayotte, "
Explanation:
parallelStream() allows parallel processing, meaning elements may be processed concurrently.
However, forEachOrdered() is used — and this preserves the encounter order of the stream, even if it's parallel.
So, even though the stream is parallel, the order of output is preserved — exactly as in the original list.
Thus, the program will always print:
Guadeloupe, Martinique, Guyane, La Réunion, Mayotte,
Just to recap the other options:
A) is incorrect because forEachOrdered maintains order, so it's not random.
C) is incorrect because no exception is thrown.
D) is incorrect because the code compiles just fine.
✅ Final Answer: B
(Working with Streams and Lambda expressions)