๐ฃ๏ธโ2๏ธโฃ1๏ธโฃ Dear followers, let's prepare for Java 21 certification together!
QUESTION 1๏ธโฃ
How would you answer this question:
Given:
What will be printed?
A) Nothing
B) Hello from France!
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
๐ก Solution
The correct answer is: B) Hello from France!
Explanation:
In the code, we have this line inside the sayHi method:
This uses pattern matching for instanceof,
a feature added in Java 16 (preview) and made standard in Java 17.
The syntax:
object instanceof String String
means: if object is an instance of String,
then cast it to a String and assign it to a variable named String.
While naming a variable String is legal (though not recommended),
it can still be compiled and executed successfully. So in this context:
- object is "Hello from France!", which is a String
- "Hello from France!" has more than 3 characters
- Therefore, the if condition is true
- System.out.println(object); is executed
And the output is:
Hello from France!
โ No runtime exceptions โ No compile error โ Output is printed
(Using Object-Oriented Concepts in Java)
LESSON 1๏ธโฃ
๐งโ๐ป๐ก Java 21 mini lesson โ Mastering switch Expressions with yield (featuring French poets ๐ซ๐ทโ๏ธ)
Javaโs modern switch expression syntax with case L -> makes your code cleaner, safer, and more expressive
โ especially when you need to return values from each branch.
Given:
โ Why this matters:
๐ฃ switch expressions return values with yield
๐ฃ case L -> avoids fall-through bugs (unlike case L:)
๐ฃ Block syntax lets you handle multiple instructions
๐ง Next time you use switch, try this lambda-like syntax โ your future self (and your teammates) will thank you!
https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A
QUESTION 2๏ธโฃ
How would you answer this question:
Given:
How many times "Hello from France!" will be printed?
A) Zero
B) Two
C) Three
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
๐ก Solution
Let's walk through the code together:
Key Points:
๐ฃ There are two nested for loops.
๐ฃ The outer loop is labeled stopOnThree.
๐ฃ Inside the inner loop, thereโs a condition: if (i + j > 3) โ then break stopOnThree; โ this exits both loops, not just the inner one.
๐ฃ The print statement is in the outer loop, after the inner loop.
Let's simulate the iterations:
Outer loop (i from 0 to 2):
๐ฃ i = 0
Inner loop (j from 0 to 2)
i + j = 0 + 0 = 0 โ OK
i + j = 0 + 1 = 1 โ OK
i + j = 0 + 2 = 2 โ OK
End of inner loop โ print โ "Hello from France!" โ
๐ฃ i = 1
i + j = 1 + 0 = 1 โ OK
i + j = 1 + 1 = 2 โ OK
i + j = 1 + 2 = 3 โ OK
End of inner loop โ print โ "Hello from France!" โ
๐ฃ i = 2
i + j = 2 + 0 = 2 โ OK
i + j = 2 + 1 = 3 โ OK
i + j = 2 + 2 = 4 โ โCondition true โ break stopOnThree; โ exits outer loop completely
So the print happens only twice, for i = 0 and i = 1.
โ Correct Answer: B) Two
The break Statement: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
(Controlling Program Flow)
QUESTION 3๏ธโฃ
How would you answer this question:
You have to create the Java module AirbusA380Plant named com.airbus.a380.plant .
The com.airbus.plant module depends on three modules:
com.airbus.toulouse.assembly,
com.airbus.broughton.wings, and
com.airbus.hamburg.fuselage
How would you define the module-info.java related file?
A)
module com.airbus.a380.plant{
import com.airbus.toulouse.assembly;
import com.airbus.broughton.wings;
import com.airbus.hamburg.fuselage;
}
B)
module com.airbus.a380.plant{
uses com.airbus.toulouse.assembly;
uses com.airbus.broughton.wings;
uses com.airbus.hamburg.fuselage;
}
C)
module com.airbus.a380.plant{
opens com.airbus.toulouse.assembly;
opens com.airbus.broughton.wings;
opens com.airbus.hamburg.fuselage;
}
D)
module com.airbus.a380.plant{
requires com.airbus.toulouse.assembly;
requires com.airbus.broughton.wings;
requires com.airbus.hamburg.fuselage;
}
E)
module com.airbus.a380.plant{
exports com.airbus.toulouse.assembly;
exports com.airbus.broughton.wings;
exports com.airbus.hamburg.fuselage;
}
#PathToJava21 #java #certification
๐ก Solution
The correct answer is:
D)
module com.airbus.a380.plant {
requires com.airbus.toulouse.assembly;
requires com.airbus.broughton.wings;
requires com.airbus.hamburg.fuselage;
}
โ Why this is correct:
The requires directive is used in a module-info.java file to declare dependencies on other modules. Since com.airbus.a380.plant depends on the three mentioned modules, it must require them.
โ Why the others are incorrect:
A)
import ...;
โ Invalid syntax in module-info.java. import is used in regular Java files, not in module descriptors.
B)
uses ...;
โ uses is for service loading, when the module is a consumer of a service defined via ServiceLoader. This is not what's described in your scenario.
C)
opens ...;
โ opens is used for reflection access, typically in frameworks like Spring. It's not for basic module dependency declaration.
E)
exports ...;
โ exports is used to expose a package from the current module to others, not to declare dependencies.
Understanding Java 9 Modules: https://www.oracle.com/corporate/features/understanding-java-9-modules.html
(Packaging and Deploying Java Code)
LESSON 2๏ธโฃ
๐งโ๐ป๐ก Java 21 mini lesson Switch and enum
๐ฝ๏ธ Java devs, did you know this about switch expressions?
Switch expressions in Java can be elegant and concise โ if you respect the rules.
Take this example using a PlatFrancais enum ๐ซ๐ท:
โ๏ธ No default needed
โ๏ธ No yield headaches
โ๏ธ Readable and expressive
โ Why does it work?
Because weโre switching on an enum and weโve covered all known constants.
The compiler sees this as exhaustive and silently adds an implicit default.
๐ก Tip: Stick to the lambda (->) style when possible โ itโs cleaner and less error-prone.
๐ท Bonus: Using French dishes as your domain model might make your code more delicious to read ๐
#Java #CodingTips #SwitchExpression #Java17 #Enum #CleanCode #FrenchFood #DeveloperLife
Prepare your Java 21 certification: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A
QUESTION 4๏ธโฃ
How would you answer this question:
Given:
What will be printed?
A) [ratatouille, bouillabaisse, coq_au_vin]
B) [ratatouille, bouillabaisse, carbonade_flamande]
C) [ratatouille, bouillabaisse, coq_au_vin, carbonade_flamande]
D) An exception is thrown at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
๐ก Solution
The correct answer is:
D) An exception is thrown at runtime.
Explanation:
You're working with a Java ArrayList and performing the following operations:
var frenchPlats = new ArrayList();
- This creates an ArrayList with raw type, which isn't ideal, but compiles.
frenchPlats.add("ratatouille");
frenchPlats.add("bouillabaisse");
frenchPlats.add("coq_au_vin");
- After these add calls, the list has 3 elements:
[ratatouille, bouillabaisse, coq_au_vin]
frenchPlats.set(3, "carbonade_flamande");
- This line tries to replace the element at index 3, but indexing is zero-based, and the current valid indices are 0, 1, and 2.
- Since there is no element at index 3, this throws an exception:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
Thus, a runtime exception is thrown, and nothing is printed.
โ Correct Answer: D
(Using Object-Oriented Concepts in Java)
LESSON 3๏ธโฃ
๐ฌ Java Record Classes Explained with French Cinema Icons! ๐ซ๐ท | Java Tutorial
Welcome to this fun and practical Java tutorial where we dive into the world of Java record classes โ using examples inspired by famous French actors like Marion Cotillard, Omar Sy, and Jean Dujardin! ๐ซ๐ทโจ
In this video, you'll learn: โ What Java record classes are
โ Why theyโre perfect for modeling simple data objects
โ How they reduce boilerplate code compared to traditional classes
โ What Java generates automatically for you (constructor, equals, toString, etc.)
โ Real-world examples using iconic French cinema figures ๐ญ
Perfect for beginners and intermediate Java developers looking to write cleaner, more expressive code!
๐ Based on JEP 395 โ Records
๐ฅ Code examples with a cinematic twist!
๐ Donโt forget to like, subscribe, and comment if you enjoyed this tutorial and want more themed Java lessons!
#Java #RecordClass #JavaTutorial #FrenchActors #LearnJava #CodingMadeFun #JEP395