Return to site

[VV104] The Java 21 Newsletter

· java21,java,ocp

🛣️☕2️⃣1️⃣ Dear followers, let's prepare for Java 21 certification together!

1️⃣ How would you answer this question:

Given:

What is printed?

  • A) 10
  • B) _$
  • C) It throws an exception.
  • D) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

The correct answer is: Compilation fails.

Explanation:

Variable Names in Java:

_ and $ are valid variable names in Java.

However, starting from Java 9, using _ as an identifier is no longer allowed because it is reserved as a keyword for future use.

Attempting to declare a variable named _ results in a compilation error.

Specific Error:

The compiler will produce an error like:

error: as of release 9, '_' is a keyword, and may not be used as an identifier

(Using Object-Oriented Concepts in Java)


2️⃣ How would you answer this question:

Given:

What is printed?

  • A) postResult: 15, preResult: 16, Final value of post: 6, Final value of pre: 6
  • B) postResult: 16, preResult: 16, Final value of post: 6, Final value of pre: 6
  • C) postResult: 15, preResult: 16, Final value of post: 5, Final value of pre: 6
  • D) postResult: 16, preResult: 15, Final value of post: 6, Final value of pre: 5

#PathToJava21 #java #certification

💡 Solution

  • Correct Answer:postResult: 15, preResult: 16, Final value of post: 6, Final value of pre: 6
  • Explanation:
  • Post-Increment (post++):

The current value of post (5) is used in the calculation (postResult = 5 + 10 = 15).

After the calculation, post is incremented to 6.

  • Pre-Increment (++pre):

The value of pre is incremented to 6 first.

The updated value (6) is then used in the calculation (preResult = 6 + 10 = 16).

  • Final Values:

After the calculations, post and pre are both incremented to 6.

Assignment, Arithmetic, and Unary Operators: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html

(Using Object-Oriented Concepts in Java)


3️⃣ How would you answer this question:

Given:

What is printed?

  • A) It prints all elements, including changes made during iteration.
  • B) It prints all elements, but changes made during iteration may not be visible.
  • C) It throws an exception.
  • D) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

  • Correct Answer:

It prints all elements, but changes made during iteration may not be visible.

  • Explanation:

Relevance of CopyOnWriteArrayList:

It is specifically designed for concurrency, providing thread safety without the need for explicit synchronization.

It works by creating a new copy of the array for every write operation, ensuring that reading threads always see a consistent snapshot of the list.

Behavior of the Code:

The writing thread (list.add("D")) modifies the list by adding an element.

The reading thread (for-each loop) iterates over a consistent snapshot of the list.

Changes made to the list ("D") during iteration will not be visible to the reading thread because the iterator uses a snapshot created when the iteration started.

  • Why Other Options Are Incorrect:

(Exception): The CopyOnWriteArrayList prevents ConcurrentModificationException by creating a snapshot for iterators, so no exception occurs.

(Compilation Fails): The program compiles and runs without issues because CopyOnWriteArrayList is designed for multithreaded use.

(Real-Time Updates): Changes made to the list are not reflected during iteration due to the snapshot behavior.

(Managing Concurrent Code Execution)


4️⃣ How would you answer this question:

Given:

What is printed?

  • A) Is Vehicle sealed? true; Is Car sealed? true; Is Bike sealed? true
  • B) Is Vehicle sealed? false; Is Car sealed? false; Is Bike sealed? false
  • C) Is Vehicle sealed? true; Is Car sealed? false; Is Bike sealed? false
  • D) Is Vehicle sealed? false; Is Car sealed? true; Is Bike sealed? true

#PathToJava21 #java #certification

💡 Solution

  • Correct Answer:

Is Vehicle sealed? true; Is Car sealed? false; Is Bike sealed? false

  • Explanation:

isSealed() Behavior:

The isSealed() method returns true if a class or interface is declared as sealed.

For other classes (e.g., non-sealed or final classes), it returns false.

Class Declarations in the Code:

Vehicle is a sealed class, so Vehicle.class.isSealed() returns true.

Car is declared as non-sealed, so Car.class.isSealed() returns false.

Bike is a final class, which is not sealed, so Bike.class.isSealed() also returns false.

  • Why the Other Options Are Incorrect:

Is Vehicle sealed? true; Is Car sealed? true; Is Bike sealed? true

-> Incorrect because Car and Bike are not sealed.

Is Vehicle sealed? false; Is Car sealed? false; Is Bike sealed? false

-> Incorrect because Vehicle is a sealed class, so it cannot return false.

Is Vehicle sealed? false; Is Car sealed? true; Is Bike sealed? true

->Incorrect because Car and Bike are explicitly non-sealed and final, not sealed.

  • Javadoc: isSealed

public boolean isSealed()

Returns true if and only if this Class object represents a sealed class or interface.

If this Class object represents a primitive type, void, or an array type, this method returns false.

A sealed class or interface has (possibly zero) permitted subclasses; getPermittedSubclasses() returns a non-null but possibly empty value for a sealed class or interface.

Returns:

true if and only if this Class object represents a sealed class or interface.

Since: 17

(Using Object-Oriented Concepts in Java)

5️⃣ How would you answer this question:

You are working on a module named perfumery.shop that depends on another module named perfumery.provider.

The perfumery.shop module should also make its package perfumery.shop.eaudeparfum available to other modules.

Which of the following is the correct file to declare the perfumery.shop module?

A)

File name: module-info.perfumery.shop.java
Contents:
module perfumery.shop {
requires perfumery.provider;
exports perfumery.shop.eaudeparfum.*;
}

B)

File name: module-info.java
Contents:
module perfumery.shop {
requires perfumery.provider;
exports perfumery.shop.eaudeparfum;
}

C)

File name: module.java
Contents:
module shop.perfumery {
requires perfumery.provider;
exports perfumery.shop.eaudeparfum;
}

#PathToJava21 #java #certification

💡 Solution

Correct Answer:

B)

File name: module-info.java
Contents:
module perfumery.shop {
requires perfumery.provider;
exports perfumery.shop.eaudeparfum;
}

Explanation:

Correct File Name:

The module declaration must be in a file named module-info.java.

Any other file name, such as module-info.perfumery.shop.java or module.java,

is invalid and will result in a compilation error.

Correct Module Name:

The module name in the declaration must match the name of the module being defined.

perfumery.shop is correct.

shop.perfumery is incorrect.

Export Statement:

To make a package available to other modules, the exports statement must specify the package name (e.g., perfumery.shop.eaudeparfum).

exports perfumery.shop.eaudeparfum; is correct.

exports perfumery.shop.eaudeparfum.*; incorrectly adds a wildcard * in the exports statement, which is not valid syntax in module-info.java.

Requires Statement:

The requires perfumery.provider; statement ensures that the perfumery.shop module depends on the perfumery.provider module.

All three options include this statement correctly.

(Packaging and Deploying Java Code)


6️⃣ How would you answer this question:

Given:

What is printed?

  • A) null
  • B) It's a string with value: 42
  • C) It's an double with value: 42
  • D) It's an integer with value: 42
  • E) It throws an exception at runtime.
  • F) Compilation fails.

#PathToJava21 #java #certification

💡 Solution

Correct Answer: Compilation fails.

Explanation:

Switch Expression Requires Exhaustiveness:

The switch expression in Java must handle all possible types of the input or include a default clause to cover unmatched cases.

In this code, the switch statement handles String, Double, and Integer, but it does not handle other types of Object (e.g., Boolean, Character, null), which leads to a compilation error.

Why Compilation Fails:

The compiler enforces that a switch expression is exhaustive.

If the input does not match any of the provided cases, the program would be unable to determine what result should be.

Adding a default case would fix this issue.

Why It Does Not Throw an Exception at Runtime:

The program does not even compile, so runtime behavior is irrelevant in this case.

Why It Does Not Print Any of the Provided:

None of these outputs occur because the code fails to compile before execution.

Fixed Code with Default Clause:

To ensure the program compiles, include a default case to handle all other types of Object:

Output:

It's an integer with value: 42

JEP 441: Pattern Matching for switch: https://openjdk.org/jeps/441

(Using Object-Oriented Concepts in Java)