🛣️☕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) Offering low-cost travel for budget-friendly journeys.
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
Let's analyze the code step by step.
Code Breakdown:
HighSpeedTrain implements two interfaces: TGVINOUI and OUIGO.
TGVINOUI is a functional interface (though not marked with @FunctionalInterface) that requires an implementation of highComfortService().
OUIGO has a default method lowCostService() — optional to override.
Does HighSpeedTrain compile?
No, it does not implement the abstract method highComfortService() from TGVINOUI.
Java requires implementing all non-default methods from interfaces unless the class is abstract.
So, compilation fails.
Correct Answer:
D) Compilation fails. ✅
(Using Object-Oriented Concepts in Java)
LESSON 1️⃣
🇫🇷 Master Java Text Blocks with French Literary Quotes 📜 | Java Tutorial
In this Java lesson, we dive into the powerful world of text blocks — a modern and cleaner way to write multi-line strings in Java. And to make it even more inspiring, we explore famous quotes from legendary French authors like Victor Hugo, Albert Camus, Voltaire, and Antoine de Saint-Exupéry.
✨ Learn how to:
🟣 Write multi-line strings without messy \n and + operators
🟣 Use text blocks in expressions and method arguments
🟣 Apply string methods to text blocks
🟣 Enhance your code readability with literary elegance
📚 Whether you're a Java beginner or seasoned dev, this tutorial will help you clean up your code and add a poetic twist to your string handling!
🧠 Quotes used:
🟣 "On ne voit bien qu’avec le cœur…" – Saint-Exupéry
🟣 "Il faut cultiver notre jardin." – Voltaire
🟣 "Il n’y a que de mauvais cultivateurs." – Victor Hugo
📌 Subscribe for more Java tips and lessons with creative real-world examples.
#Java #TextBlocks #FrenchAuthors #VictorHugo #SaintExupéry #Camus #Voltaire #JavaTips #CodeClean #LearnJava #javadeveloperjobs
Prepare for 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 is printed?
A) Suite
B) DeluxeSeaViewSuite[number=42]
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
The correct answer is:
D) Compilation fails.
Explanation:
Java records cannot extend classes.
They implicitly extend java.lang.Record, and this inheritance is final
— meaning a record cannot extend any other class.
So this line is invalid:
record DeluxeSeaViewSuite(int number) extends Suite {
It will cause a compilation error, such as:
Cannot extend a class in a record declaration
Even if the class Suite exists and has a toString() method,
a record cannot use inheritance to access or override it.
Summary:
✅ Records can implement interfaces.
❌ Records cannot extend classes.
Hence, compilation fails — answer D is correct.
Programmer's Guide to Text Blocks: https://docs.oracle.com/en/java/javase/21/text-blocks/index.html
(Handling Date, Time, Text, Numeric and Boolean Values)
QUESTION 3️⃣
How would you answer this question:
Given:
What is printed?
A) [Character][Character][Character]
B) [Character][Integer][Integer]
C) [Character][Integer][Default]
D) [Default][Default][Default]
#PathToJava21 #java #certification
💡 Solution
✅ Correct answer: C) [Character][Integer][Default]
Breakdown:
First call: print('1')
'1' is a char literal.
Java performs autoboxing and converts char to Character.
So o is a Character.
Pattern matching switch matches case Character c → ✅
Prints: [Character]
Second call: print(1)
1 is an int literal.
Autoboxed to Integer.
So o is an Integer.
Matches case Integer i → ✅
Prints: [Integer]
Third call: print(1L)
1L is a long literal.
Autoboxed to Long.
So o is a Long.
Long does not match any of the case patterns.
Falls into default.
Prints: [Default]
Final Output:
[Character][Integer][Default]
✅ Correct answer: C) [Character][Integer][Default]
(Controlling Program Flow)
LESSON 2️⃣
🔒 Java Sealed Classes Explained with Carrefour Store Types | Java 17+ Tutorial
📺 video: https://youtu.be/dXo3vbCVaac
✍️ article: https://blog.vvauban.com/j21-sealed
In this video, we dive into Java sealed classes using a fun and practical analogy: the different types of Carrefour shops (City, Market, Hyper, Drive, Bio…). 🛒
You'll learn:
🟣 What sealed classes and interfaces are 🧠
🟣 Why and when to use them in real-world applications ⚙️
🟣 How to declare sealed, non-sealed, and final subclasses 💡
🟣 The benefits of sealed hierarchies, especially for domain modeling and exhaustive switch statements ✅
Using Carrefour’s real-world store structure, we illustrate how to control inheritance and keep your object-oriented design clean and safe.
🔧 Java version: 17+
📦 Concept: JEP 409 – Sealed Classes
📚 Code examples used:
public sealed class CarrefourShop permits CarrefourCity, CarrefourMarket, CarrefourHyper { }
…and more!
👍 Like this lesson? Subscribe for more Java concepts explained with real-world analogies!
#Java #SealedClasses #Carrefour #Java17 #OOP #JavaTutorial #objectorientedprogramming
Prepare for Java certification: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A
QUESTION 4️⃣
How would you answer this question:
Given:
Which methods compile?
A) pw1()
B) pw2()
C) pw3()
D) pw4()
#PathToJava21 #java #certification
💡 Solution
Let's go through each method and analyze whether it compiles.
✅ pw1()
✅ PrintWriter.write(String) is valid.
✅ checkError() returns a boolean.
✅ Everything compiles.
✅ pw1() compiles.
❌ pw2()
⚠️ PrintWriter.write(String) does not throw IOException, it catches IOExceptions internally and uses checkError() instead.
❌ So the catch (IOException ioe) block causes a compiler error: "exception never thrown in body of corresponding try statement."
❌ pw2() does not compile.
❌ pw3()
⚠️ writer.write(String) returns void, not boolean.
❌ Assigning it to a boolean causes a compile-time error.
❌ pw3() does not compile.
✅ pw4()
✅ printf() returns PrintWriter, so chaining with print() is valid.
✅ All methods exist and are used correctly.
✅ pw4() compiles.
✅ Final Answer:
The methods that compile are:
A) pw1()
D) pw4()
✅ Correct answer: A and D
Character Streams: https://docs.oracle.com/javase/tutorial/essential/io/charstreams.html
(Using Java I/O API)