🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!
1️⃣ How would you answer this question:
Which of the following methods of java.util.function.Predicate are default methods?
Select 3.
- A) and(Predicate other)
- B) isEqual(Object targetRef)
- C) negate()
- D) not(Predicate target)
- E) or(Predicate other)
- F) test(T t)
#PathToJava21 #java #certification
💡 Solution
In the java.util.function.Predicate interface, the default methods are methods that come with a default implementation in the interface itself.
The following are the correct answers:
✅ A) and(Predicate other) → Default method
✅ C) negate() → Default method
✅ E) or(Predicate other) → Default method
Explanation:
- and(Predicate other) → ✅ Default method
Defined in Predicate with a default implementation that returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
- isEqual(Object targetRef) → ❌ Static method
This is a static method in Predicate, not a default method.
- negate() → ✅ Default method
Returns a predicate that represents the logical negation of this predicate.
- not(Predicate targetRef) → ❌ Static method (since Java 11)
This is a static method, not a default method.
- or(Predicate other) → ✅ Default method
Returns a predicate that represents a short-circuiting logical OR of this predicate and another.
- test(T t) → ❌ Abstract method
This is the primary functional method of Predicate, and it is abstract, not a default method.
Correct answers:
✅ A) and(Predicate other)
✅ C) negate()
✅ E) or(Predicate other)
(Working with Streams and Lambda expressions)
2️⃣ How would you answer this question:
Given:
Which of the following statements,
inserted in the code given above,
prints both: "Executing Task-2" and "Executing Task-1"?
A)
execService.call(task1);
execService.call(task2);
B)
execService.execute(task1);
execService.execute(task2);
C)
execService.run(task1);
execService.run(task2);
D)
execService.submit(task1);
execService.submit(task2);
#PathToJava21 #java #certification
💡 Solution
The execService is an ExecutorService, which is used to manage threads in a thread pool.
The goal is to execute both tasks and ensure their corresponding print statements appear.
Examining Each Option:
- A) execService.call(task1); execService.call(task2);
Incorrect ❌
ExecutorService does not have a call method.
This will result in a compilation error.
- B) execService.execute(task1); execService.execute(task2);
Incorrect ❌
execute() is only available for Runnable, not for Callable.
task1 (Runnable) will be executed successfully.
task2 (Callable) cannot be passed to execute(), so this will result in a compilation error.
- C) execService.run(task1); execService.run(task2);
Incorrect ❌
ExecutorService does not have a run method.
This will result in a compilation error.
- D) execService.submit(task1); execService.submit(task2);
Correct ✅
submit(Runnable) will execute task1.
submit(Callable) will execute task2.
Both tasks will be executed by the thread pool.
Output:
Executing Task-1
Executing Task-2
Correct Answer:
✅ Only D is correct.
(Managing Concurrent Code Execution)
3️⃣ How would you answer this question:
Given:
What is printed?
A)
Saint-Émilion
B)
Beaujolais Nouveau, Chablis, Saint-Émilion
C)
Beaujolais Nouveau, Chablis, Dom Pérignon, Saint-Émilion
D)
Rosé
#PathToJava21 #java #certification
💡 Solution
Let's analyze the code step by step.
- Step 1: main() method execution
The main() method starts executing and enters the try block.
It calls thrower().
- Step 2: thrower() method execution
Inside thrower(), we have:
int i = 0;
return i / i;
This results in a division by zero (ArithmeticException).
Since thrower() only catches NumberFormatException, the ArithmeticException is not caught and propagates back to the caller (main()).
- Step 3: finally block in thrower()
The finally block always executes:
System.out.print("Beaujolais Nouveau, ");
This prints:
"Beaujolais Nouveau, "
After this, thrower() does not return a value because the exception propagates.
- Step 4: Handling the Exception in main()
The ArithmeticException is caught in main()'s catch block:
catch ( Exception e ) {
System.out.print( "Chablis, " );
}
This prints:
"Chablis, "
- Step 5: finally block in main()
The finally block in main() executes:
finally {
System.out.print("Saint-Émilion");
}
This prints:
"Saint-Émilion"
Final Output
The sequence of prints is:
Beaujolais Nouveau, Chablis, Saint-Émilion
Correct Answer:
B) Beaujolais Nouveau, Chablis, Saint-Émilion
The catch Blocks: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html
[JLS] 11.3. Run-Time Handling of an Exception: https://docs.oracle.com/javase/specs/jls/se8/html/jls-11.html#jls-11.3
(Handling Exceptions)
4️⃣ How would you answer this question:
Given:
What is printed?
A)
Hall of Mirrors has 17 mirrors.
The gardens cover 800 hectares.
B) Nothing
C) An exception is thrown at runtime.
D) Compilation fails at line n1.
E) Compilation fails at line n2.
#PathToJava21 #java #certification
💡 Solution
Explanation:
At first glance, it may seem that Versailles() is a constructor, but it's actually a regular method because it has a return type (void).
In Java, constructors must not have a return type—not even void.
Since no valid constructor is explicitly defined, the Java compiler provides a default no-argument constructor:
public Versailles() {} // Default constructor added by the compiler
This default constructor does nothing when an object of Versailles is created.
Now, let's analyze what happens in the main method:
var castle = new Versailles(); // Calls the default constructor
Since the default constructor does not initialize any fields or call any methods, nothing is printed to the console.
The method void Versailles() is never called anywhere in the program, so its code (which prints text) is never executed.
Correct Answer:
B) Nothing.
This is a great example of why constructors should not have a return type and how Java handles missing constructors by providing a default one.
Providing Constructors for Your Classes: https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
(Using Object-Oriented Concepts in Java)
5️⃣ How would you answer this question:
Given:
What is printed?
A)
Quand il me prend dans ses bras
Qu'il me parle tout bas
Je vois la vie en rose
B) Nothing
C) An exception is thrown at runtime.
D) Compilation fails.
#PathToJava21 #java #certification
💡 Solution
- Step 1: Identifying the Issue
At first glance, this code appears to iterate over the lines of a string and print them.
However, there is a syntax error in the for loop:
for ( int i = 0, int j = 3; i < j; i++ ) {
The issue is how int is used in the initialization part of the loop.
- Step 2: Understanding Variable Declaration Rules
In Java, when declaring multiple variables of the same type in a for loop, the correct syntax is:
for (int i = 0, j = 3; i < j; i++) { ... }
However, the given code incorrectly repeats int before j:
for (int i = 0, int j = 3; i < j; i++) { ... }
This is a compilation error because Java does not allow redeclaring the type like this in a for loop
- Step 3: Correcting the Code
To fix the syntax error, the loop should be written as:
for (int i = 0, j = 3; i < j; i++) {
With this fix, the program would compile and execute as expected.
Final Answer
Since the original code contains a syntax error, the correct choice is:
✅ D) Compilation fails.
[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)
6️⃣ How would you answer this question:
Given:
A module com.eiffeltower.shop with the related sources in src directory.
That module requires com.eiffeltower.membership available in a jar located in the lib directory.
What is the command to compile the module com.eiffeltower.shop ?
- A) javac -source src -p lib/com.eiffel.membership.jar -d out -m com.eiffeltower.shop
- B) javac --module-source-path src -p lib/com.eiffel.membership.jar -d out -m com.eiffeltower.shop
- C) javac --module-source-path src -p lib/com.eiffel.membership.jar -s out -m com.eiffeltower.shop
- D) javac -path src -p lib/com.eiffel.membership.jar -d out -m com.eiffeltower.shop
#PathToJava21 #java #certification
💡 Solution
The correct answer is:
✅ B)
javac --module-source-path src -p lib/com.eiffel.membership.jar -d out -m com.eiffeltower.shop
Explanation:
- --module-source-path src
Specifies that src/ contains modular source code.
Used for compiling multiple modules in a project.
- -p lib/com.eiffel.membership.jar
-p (or --module-path) specifies where to find the required module (membership.jar).
However, it should be -p lib instead of -p lib/com.eiffel.membership.jar, because lib is the module path, not the JAR file itself.
- -d out
Specifies the output directory for compiled .class files.
- -m com.eiffeltower.shop
Indicates which module should be compiled.
Why the Other Options Are Incorrect:
A) -source src ❌ → There is no -source flag in this context; it should be --module-source-path src.
C) -s out ❌ → -s is for generated source files, not compiled .class files.
D) -path src ❌ → There is no -path flag; --module-source-path should be used instead.
[Oracle doc] javac command: https://docs.oracle.com/javase/9/tools/javac.htm#JSWOR627
Doc:
--source-path path or -sourcepath path
Specifies where to find input source files.
This is the source code path used to search for class or interface definitions.
As with the user class path, source path entries are separated by colons (:) on Oracle Solaris and semicolons(;) on Windows.
They can be directories, JAR archives, or ZIP archives.
If packages are used, then the local path name within the directory or archive must reflect the package name.
--class-path path , -classpath path, or -cp path
Specifies where to find user class files and annotation processors.
This class path overrides the user class path in the CLASSPATH environment variable.
If --class-path, -classpath, or -cp aren’t specified, then the user class path is the current directory.
If the -sourcepath option isn’t specified, then the user class path is also searched for source files.
If the -processorpath option isn’t specified, then the class path is also searched for annotation processors.
-d directory
Sets the destination directory for class files.
If a class is part of a package, then javac puts the class file in a subdirectory that reflects the package name and creates directories as needed.
For example:
Oracle Solaris, Linux, and OS X:
If you specify -d /home/myclasses and the class is called com.mypackage.MyClass,
then the class file is /home/myclasses/com/mypackage/MyClass.class.
Windows:
If you specify -d C:\myclasses and the class is called com.mypackage.MyClass,
then the class file is C:\myclasses\com\mypackage\MyClass.class.
If the -d option isn’t specified,
then javac puts each class file in the same directory as the source file from which it was generated.
--module-source-path module-source-path
Specifies where to find input source files for multiple modules.
--module-path path or -p path
Specifies where to find application modules.
--module module-name or -m module-name
Compiles only the specified module and checks time stamps.
-s directory
Specifies the directory used to place the generated source files.
If a class is part of a package, then the compiler puts the source file in a subdirectory that reflects the package name and creates directories as needed.
For example:
Oracle Solaris, Linux, and OS X:
If you specify -s /home/mysrc and the class is called com.mypackage.MyClass,
then the source file is put in /home/mysrc/com/mypackage/MyClass.java.
Windows:
If you specify -s C:\mysrc and the class is called com.mypackage.MyClass,
then the source file is put in C:\mysrc\com\mypackage\MyClass.java.
-source release
Specifies the version of source code accepted.(ie: 8 or 1.8)
(Using Object-Oriented Concepts in Java)