🛣️☕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) 0
B) 2
C) 5
D) It throws an exception at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Alright, let’s go through it carefully:
In your main method:
int baguettes, croissants=5;
System.out.println(baguettes);
baguettes is declared but not initialized locally inside main.
In Java, local variables must be explicitly initialized before use.
Trying to print baguettes without initializing it causes a compilation error
— even though there is a static int baguettes = 2; in the class.
👉 Local variable baguettes hides the class variable.
👉 And it is uninitialized when printed.
Therefore, the correct answer is:
✅ E) Compilation fails.
Local Variable Declaration Statements: https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-14.4
(Using Object-Oriented Concepts in Java)
L️ESSON 1️⃣
☕🧱 Java Textblock Syntax #minilesson
🇫🇷✨ Master Java Text Blocks with French Literary Flair! ✨🇫🇷
In this video, we explore Java's powerful Text Block feature through the elegant words of iconic French authors like Victor Hugo, Antoine de Saint-Exupéry, and more!
📚 You'll learn:
🟣 The syntax and rules of Java text blocks
🟣 Common mistakes to avoid
🟣 How text blocks simplify multi-line strings
🟣 How to include famous French quotes in your Java code with ease
🟣 Bonus: See how Java text blocks can even hold code snippets!
💬 Example Quotes Used:
🟣 "La liberté commence où l’ignorance finit." — Victor Hugo
🟣 "On ne voit bien qu’avec le cœur." — Antoine de Saint-Exupéry
Whether you're a Java beginner or brushing up your syntax, this is a poetic and practical guide to writing cleaner, more readable string literals in Java 15 and beyond.
👇 Don’t forget to like, comment, and subscribe for more Java tips with a creative twist!
#Java #Programming #TextBlocks #FrenchLiterature #VictorHugo #SaintExupéry #CodeWithStyle
Prepare the Java certification: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A
QUESTION 2️⃣
How would you answer this question:
Given:
What will be printed?
A) 'Eiffel tower' followed by an exception
B) 'Eiffel tower' then 'Optional.empty'
C) 'Optional.empty' then 'Arc de Triomphe'
D) It throws an exception at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Let's walk through it carefully:
var monuments = new ArrayList();
monuments.add("Eiffel tower"); // index 0
monuments.add(null); // index 1
monuments.add("Arc de Triomphe"); // index 2
Optional oneMonument = Optional.ofNullable(monuments.get(1));
System.out.println(oneMonument.get());
Optional anotherMonument = Optional.ofNullable(monuments.get(2));
System.out.println(anotherMonument.get());
🟣 monuments.get(1) is null. So oneMonument will be Optional.empty.
🟣 Then oneMonument.get() tries to get the value inside the Optional.
👉 But calling get() on an empty Optional throws a NoSuchElementException at runtime!
Thus, an exception is thrown when executing oneMonument.get(), before even reaching the second part (anotherMonument.get()).
Therefore, the correct answer is:
D) It throws an exception at runtime.
(Using Object-Oriented Concepts in Java)
QUESTION 3️⃣
How would you answer this question:
Given:
What will be printed?
A)
[boo, :::, and::foo]
[boo, :::, and, ::, foo]
[boo, :::, and, ::, foo]
B)
[boo, :::]
[boo, :::, and, ::, foo]
[]
C)
[boo, :::]
[boo, :::, and, ::, foo]
[boo, :::, and, ::, foo]
D) It throws an exception at runtime.
E) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
The key points of splitWithDelimiters in Java 21:
🟣 It splits around matches of the regex.
🟣 It returns both the substrings and the delimiters.
🟣 The delimiters are included separately in the result array.
🟣 The limit works like this:
- Positive limit → apply the regex at most limit - 1 times.
- Zero → as many times as possible but remove trailing empty strings.
- Negative → as many times as possible, keeping everything.
Given string:
var string = "boo:::and::foo";
Regex:
":+"
(matching one or more colons).
Let's walk through each line:
First line:
System.out.println(Arrays.asList(string.splitWithDelimiters(":+", 2)));
🟣 Limit = 2 → apply regex at most 1 time (limit - 1 = 1).
🟣 So only one split.
🟣 First match: after "boo", there is ":::"
🟣 Therefore:
- "boo"
- ":::"
- Remaining (after the first split, everything else): "and::foo"
Result:
["boo", ":::", "and::foo"]
Second line:
System.out.println(Arrays.asList(string.splitWithDelimiters(":+", 5)));
🟣 Limit = 5 → apply regex at most 4 times.
🟣 We split at each :+ until 4 splits maximum.
Walking step-by-step:
🟣 "boo" → match ":::"
🟣 then "and" → match "::"
🟣 then "foo"
Result:
["boo", ":::", "and", "::", "foo"]
(Exactly like in your documentation example.)
Third line:
System.out.println(Arrays.asList(string.splitWithDelimiters(":+", -1)));
🟣 Limit = -1 → unlimited splits, as much as possible.
Same behavior as limit 5 here (because we don't hit any limit before the string ends):
["boo", ":::", "and", "::", "foo"]
Thus the output will be:
[boo, :::, and::foo]
[boo, :::, and, ::, foo]
[boo, :::, and, ::, foo]
✅
Matching the choices:
A)
[boo, :::, and::foo]
[boo, :::, and, ::, foo]
[boo, :::, and, ::, foo]
✔️ matches!
🟣 Other choices (B, C, D, E) don't match.It splits around matches of the regex.
(Handling Date, Time, Text, Numeric and Boolean Values)