[VV112] The Java 21 Newsletter
[VV112] The Java 21 Newsletter

[VV112] The Java 21 Newsletter

????2??1?? Dear followers, let's prepare for Java 21 certification together!

1?? How would you answer this question:

Which of the following is not a module directive in the Java Platform Module System?

A) provides

B) uses

C) opens

D) exports

E) requires

F) depends

#PathToJava21 #java #certification

?? More: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

?? Solution

The correct answer is:

F) depends

In the Java Platform Module System (JPMS), the following are valid module directives:

requires – Specifies a dependency on another module.

exports – Makes a package accessible to other modules.

opens – Makes a package accessible for deep reflection (e.g., with reflection APIs).

uses – Specifies a service interface that the module consumes.

provides – Specifies a service implementation for a service interface.

However, depends is not a valid module directive in Java.

[JLS] Module Declarations : https://docs.oracle.com/javase/specs/jls/se21/html/jls-7.html#jls-7.7

(Packaging and Deploying Java Code)





2?? How would you answer this question:

Given:

String filePath = "monuments.txt";
BufferedReader reader = null;
try ( reader =new BufferedReader(new FileReader(filePath ))){
    String line;
    while ( (line = reader.readLine()) != null ) {
        System.out.println( line );
    }
} catch(IOException e){
    System.err.println( "Error reading the file: " + e.getMessage() );
}        

and monuments.txt file is in the right location,

and monuments.txt file contains:

Eiffel Tower
Arc de Triomphe
Notre-Dame de Paris
Palace of Versailles
Mont Saint-Michel         

A) It prints the content of monuments.txt

B) It runs successfully without printing anything.

C) It throws an exception.

D) Compilation fails.

#PathToJava21 #java #certification

?? More: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

?? Solution

The provided code contains an issue in the try block:

try ( reader = new BufferedReader(new FileReader(filePath)) ) {        

This is incorrect because in a try-with-resources statement, the resource must be declared inside the parentheses.

The correct syntax would be:

try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {        

Since the code does not follow this rule, it fails to compile.

Correct Answer:

D) Compilation fails.

LocalVariableDeclaration: https://docs.oracle.com/javase/specs/jls/se21/html/jls-14.html#jls-LocalVariableDeclaration

The try-with-resources Statement: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

(Handling Exceptions)



3?? How would you answer this question:

Given:

List<Integer> phoneCountryCode = List.of( 1, 32, 33, 34, 44, 49 );
<INSERT CODE HERE>
phoneCountryCode.forEach( i -> {
    if ( onlyFrance.test( i ) ) {
        System.out.println( "France international dialing code is in the list: " + i );
    }
} );         

Which of the following suggestions prints :

"France international dialing code is in the list: 33" ? (Choose 2)

A) UnaryOperator<Integer> onlyFrance = i -> 33;

B) IntPredicate onlyFrance = i -> i == 33;

C) Function<Integer, Boolean> onlyFrance = i -> i == 33;

D) Predicate<Integer> onlyFrance = i -> i == 33;

#PathToJava21 #java #certification

?? More: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

?? Solution

Let's analyze each option carefully and determine which two will correctly print:

"France international dialing code is in the list: 33"

Given Code:

List<Integer> phoneCountryCode = List.of(1, 32, 33, 34, 44, 49);
<INSERT CODE HERE>
phoneCountryCode.forEach( i -> {
    if ( onlyFrance.test( i ) ) {
        System.out.println( "France international dialing code is in the list: " + i );
    }
} );        

Breakdown:

The forEach method iterates over the phoneCountryCode list.

onlyFrance.test(i) must return true for i == 33 to print the desired output.

The onlyFrance variable must be of a type that has a test method.

Evaluating the Options:

A) UnaryOperator<Integer> onlyFrance = i -> 33;

?? Incorrect

UnaryOperator<T> is a functional interface for a function that takes an argument and returns a value of the same type.

The expression i -> 33 always returns 33, regardless of i, meaning test(i) doesn’t exist.

Compilation error (UnaryOperator does not have test).

B) IntPredicate onlyFrance = i -> i == 33;

? Correct

IntPredicate is a functional interface specifically for primitive int values.

test(int value) method exists.

i == 33 will return true only for i = 33, leading to the expected output.

C) Function<Integer, Boolean> onlyFrance = i -> i == 33;

?? Incorrect

Function<T, R> is used for transforming a value of type T into a return value of type R.

The function returns a Boolean, but Function does not have a test method.

Compilation error (Function does not have test).

D) Predicate<Integer> onlyFrance = i -> i == 33;

? Correct

Predicate<T> is a functional interface that defines test(T t), which returns a boolean.

i -> i == 33 properly checks if i is 33 and returns true for 33.

Correctly prints the expected message.

Final Answer:

? B) IntPredicate onlyFrance = i -> i == 33;

? D) Predicate<Integer> onlyFrance = i -> i == 33;

IntPredicate: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/function/IntPredicate.html

Predicate: https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/function/Predicate.html

(Working with Streams and Lambda expressions)





4?? How would you answer this question:

In Java Platform Module System, which of the following statements are true? (Choose 2)

A) At compilation, the module-info.java becomes a ".properties" file

B) module-info.java file can have only one module name

C) module-info.java file must not be empty

D) module-info.java file can be located inside any folder

#PathToJava21 #java #certification

?? More: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

?? Solution

The correct answers are:

B) module-info.java file can have only one module name

C) module-info.java file must not be empty

Explanation:

A) At compilation, the module-info.java becomes a ".properties" file ?

Incorrect. When compiled, module-info.java is transformed into module-info.class, not a .properties file.

B) module-info.java file can have only one module name ?

Correct. A module-info.java file defines exactly one module. You cannot declare multiple modules in a single file.

C) module-info.java file must not be empty ?

Correct. The module-info.java file must contain at least the module moduleName {} declaration. An empty file would cause a compilation error.

D) module-info.java file can be located inside any folder ?

Incorrect. The module-info.java file must be placed at the root of the src/<module-name>/ directory to be recognized by the Java compiler.

Understanding Java 9 Modules : https://www.oracle.com/corporate/features/understanding-java-9-modules.html

(Packaging and Deploying Java Code)




5?? How would you answer this question:

Given:

int frenchPopulation = 68_000_000;
//France's estimated population growth rate in 2025 is 0.25%
long frenchPopulationIn2020 = frenchPopulation * Math.exp( 0.0025 * 5 );
System.out.println( String.format(
                            "Estimated population in 2030: %.1f million",
                            frenchPopulationIn2020 / 1_000_000 )
                  );        

What happens?

A) It prints: "Estimated population in 2030: 68.9 million"

B) It prints: "Estimated population in 2030: 69 million"

C) It throws an exception at runtime.

D) Compilation fails.

#PathToJava21 #java #certification

?? More: https://www.udemy.com/course/ocp-oracle-certified-professional-java-developer-prep/?referralCode=54114F9AD41F127CB99A

?? Solution

The correct answer is D) Compilation fails.

Explanation:

The error occurs because you're trying to assign the result of frenchPopulation Math.exp(0.0025 5) to a long variable,

frenchPopulationIn2020, but Math.exp() returns a double.

Java does not allow implicit conversion from double to long due to the potential for loss of precision.

You will get a compilation error like this:

java: incompatible types: possible lossy conversion from double to long

How to fix:

You can either:

Change frenchPopulationIn2020 to double:

double frenchPopulationIn2020 = frenchPopulation Math.exp(0.0025 5);

Or explicitly cast the result to long:

long frenchPopulationIn2020 = (long) (frenchPopulation Math.exp(0.0025 5));

Once you make the change, the code will compile and run correctly.

[JLS] 5.1.2. Widening Primitive Conversion: https://docs.oracle.com/javase/specs/jls/se21/html/jls-5.html#jls-5.1.2

19 specific conversions on primitive types are called the widening primitive conversions:

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

long to float or double

float to double

(Handling Date, Time, Text, Numeric and Boolean Values)





6?? How would you answer this question:

Given:

double[] arrayEven = { 0, 2, 4, 6, 8 };

double[] arrayOdd = { 1, 3, 5, 7, 9 };

var compareResult = Arrays.compare( arrayEven, arrayOdd );

var mismatchResult = Arrays.mismatch( arrayEven, arrayOdd );

System.out.println( compareResult + " " + mismatchResult );        

What is printed?

A) 0 0

B) -1 0

C) 1 0

D) It throws an exception at runtime.

E) Compilation fails.

hashtag#PathToJava21 hashtag#java hashtag#certification

?? More: https://lnkd.in/eZKYX5hP

?? Solution

Understanding Arrays.compare()

Arrays.compare(arrayEven, arrayOdd) performs a lexicographical comparison:

It compares elements pairwise until a difference is found.

If all elements are equal, it returns 0.

If the first differing element in arrayEven is smaller, it returns -1.

If the first differing element in arrayEven is greater, it returns 1.

Step-by-step comparison:

0 (from arrayEven) vs 1 (from arrayOdd) → 0 < 1

Since 0 is less than 1, Arrays.compare() immediately returns -1.

Understanding Arrays.mismatch()

Arrays.mismatch(arrayEven, arrayOdd) finds the first index where elements differ.

If arrays are identical, it returns -1.

Otherwise, it returns the index of the first mismatching element.

Step-by-step mismatch check:

Index 0: 0 vs 1 (Mismatch found)

So, Arrays.mismatch() returns 0.

Final Output:

System.out.println(compareResult + " " + mismatchResult);

compareResult = -1

mismatchResult = 0

The printed output is:

-1 0

Correct Answer:

B) -1 0

Arrays: https://lnkd.in/eaGTvN-p

(Working with Arrays and Collections)




要查看或添加评论,请登录

Vincent Vauban的更多文章

  • ?? Broken Function Level Authorization – API5:2023 ??

    ?? Broken Function Level Authorization – API5:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • FR Les mots sans les maux. ???

    FR Les mots sans les maux. ???

    FR Hier, j’ai eu la chance d’assister à un atelier sur la Communication Non Violente (CNV) avec les superbes people de…

  • ?? Unrestricted Resource Consumption – API4:2023 ??

    ?? Unrestricted Resource Consumption – API4:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • ?? Broken Object Property Level Authorization – API3:2023 ??

    ?? Broken Object Property Level Authorization – API3:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • [VV111] The Java 21 Newsletter

    [VV111] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

    18 条评论
  • ?? Broken Authentication – API2:2023 ??

    ?? Broken Authentication – API2:2023 ??

    I'm kicking off a series of articles on API Security ?? to help us—developers ????????—better understand and implement…

  • ?? BOLA – The #1 API Security Threat (API1:2023)

    ?? BOLA – The #1 API Security Threat (API1:2023)

    I'm kicking off a series of articles on API Security ?? to help us—developers ??????????—better understand and…

  • [VV110] The Java 21 Newsletter

    [VV110] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

  • ?2??4?? Java 24 features with Thiago

    ?2??4?? Java 24 features with Thiago

    (Thanks Thiago Gonzaga ) Here are some insights based on Thiago X content. Java 24: JEP 491 Boosts Virtual Threads! ??…

  • [VV109] The Java 21 Newsletter

    [VV109] The Java 21 Newsletter

    ????2??1?? Dear followers, let's prepare for Java 21 certification together! 1?? How would you answer this question:…

社区洞察