Understanding Optional.ofNullable(null) * Optional.ofNullable(null); creates an empty Optional (i.e., it contains no value). * Optional.of(null); would throw a NullPointerException, but ofNullable(null); safely creates an empty Optional. Execution of orElse, orElseGet, and orElseThrow * orElse("Baguette") * Since optionalName is empty, "Baguette" is returned. * bread = "Baguette" * Output:"bread:Baguette" * orElseGet(() -> "Frog legs") * Since optionalName is empty, "Frog legs" is returned from the lambda expression. * dish = "Frog legs" * Output:", dish:Frog legs" * orElseThrow(() -> new Exception()) * Since optionalName is empty, an exception is thrown. * The catch block catches this exception and prints ", no cheese.". Thus, the final output is: makefile bread:Baguette, dish:Frog legs, no cheese. References: * Java SE 21 & JDK 21 - Optional * Java SE 21 - Functional Interfaces
Question 2
Which of the following statements is correct about a final class?
Correct Answer: C
In Java, the final keyword can be applied to classes, methods, and variables to impose certain restrictions. Final Classes: * Definition:A class declared with the final keyword is known as a final class. * Purpose:Declaring a class as final prevents it from being subclassed. This is useful when you want to ensure that the class's implementation remains unchanged and cannot be extended or modified through inheritance. Option Evaluations: * A. The final keyword in its declaration must go right before the class keyword. * This is correct. The syntax for declaring a final class is: java public final class ClassName { // class body } * However, this statement is about syntax rather than the core characteristic of a final class. * B. It must contain at least a final method. * Incorrect. A final class can have zero or more methods, and none of them are required to be declared as final. The final keyword at the class level prevents inheritance, regardless of the methods' finality. * C. It cannot be extended by any other class. * Correct. The primary characteristic of a final class is that it cannot be subclassed. Attempting to do so will result in a compilation error. * D. It cannot implement any interface. * Incorrect. A final class can implement interfaces. Declaring a class as final restricts inheritance but does not prevent the class from implementing interfaces. * E. It cannot extend another class. * Incorrect. A final class can extend another class. The final keyword prevents the class from being subclassed but does not prevent it from being a subclass itself. Therefore, the correct statement about a final class is option C: "It cannot be extended by any other class."
Question 3
Given: java DoubleSummaryStatistics stats1 = new DoubleSummaryStatistics(); stats1.accept(4.5); stats1.accept(6.0); DoubleSummaryStatistics stats2 = new DoubleSummaryStatistics(); stats2.accept(3.0); stats2.accept(8.5); stats1.combine(stats2); System.out.println("Sum: " + stats1.getSum() + ", Max: " + stats1.getMax() + ", Avg: " + stats1.getAverage()); What is printed?
Correct Answer: A
The DoubleSummaryStatistics class in Java is part of the java.util package and is used to collect and summarize statistics for a stream of double values. Let's analyze how the methods work: * Initialization and Data Insertion * stats1.accept(4.5); # Adds 4.5 to stats1. * stats1.accept(6.0); # Adds 6.0 to stats1. * stats2.accept(3.0); # Adds 3.0 to stats2. * stats2.accept(8.5); # Adds 8.5 to stats2. * Combining stats1 and stats2 * stats1.combine(stats2); merges stats2 into stats1, resulting in one statistics summary containing all values {4.5, 6.0, 3.0, 8.5}. * Calculating Output Values * Sum= 4.5 + 6.0 + 3.0 + 8.5 = 22.0 * Max= 8.5 * Average= (22.0) / 4 = 5.5 Thus, the output is: yaml Sum: 22.0, Max: 8.5, Avg: 5.5 References: * Java SE 21 & JDK 21 - DoubleSummaryStatistics * Java SE 21 - Streams and Statistical Operations
Question 4
Given: java int post = 5; int pre = 5; int postResult = post++ + 10; int preResult = ++pre + 10; System.out.println("postResult: " + postResult + ", preResult: " + preResult + ", Final value of post: " + post + ", Final value of pre: " + pre); What is printed?
Correct Answer: A
* Understanding post++ (Post-increment) * post++uses the value first, then increments it. * postResult = post++ + 10; * post starts as 5. * post++ returns 5, then post is incremented to 6. * postResult = 5 + 10 = 15. * Final value of post after this line is 6. * Understanding ++pre (Pre-increment) * ++preincrements the value first, then uses it. * preResult = ++pre + 10; * pre starts as 5. * ++pre increments pre to 6, then returns 6. * preResult = 6 + 10 = 16. * Final value of pre after this line is 6. Thus, the final output is: yaml postResult: 15, preResult: 16, Final value of post: 6, Final value of pre: 6 References: * Java SE 21 - Operators and Expressions * Java SE 21 - Arithmetic Operators
Question 5
How would you create a ConcurrentHashMap configured to allow a maximum of 10 concurrent writer threads and an initial capacity of 42? Which of the following options meets this requirement?
Correct Answer: E
In Java, the ConcurrentHashMap class provides several constructors that allow for the customization of its initial capacity, load factor, and concurrency level. To configure a ConcurrentHashMap with an initial capacity of 42 and a concurrency level of 10, you can use the following constructor: java public ConcurrentHashMap(int initialCapacity, float loadFactor, int concurrencyLevel) Parameters: * initialCapacity: The initial capacity of the hash table. This is the number of buckets that the hash table will have when it is created. In this case, it is set to 42. * loadFactor: A measure of how full the hash table is allowed to get before it is resized. The default value is 0.75, but in this case, it is set to 0.88. * concurrencyLevel: The estimated number of concurrently updating threads. This is used as a hint for internal sizing. In this case, it is set to 10. Therefore, to create a ConcurrentHashMap with an initial capacity of 42, a load factor of 0.88, and a concurrency level of 10, you can use the following code: java var concurrentHashMap = new ConcurrentHashMap<>(42, 0.88f, 10); Option Evaluations: * A. var concurrentHashMap = new ConcurrentHashMap(42);: This constructor sets the initial capacity to 42 but uses the default load factor (0.75) and concurrency level (16). It does not meet the requirement of setting the concurrency level to 10. * B. None of the suggestions.: This is incorrect because option E provides the correct configuration. * C. var concurrentHashMap = new ConcurrentHashMap();: This uses the default constructor, which sets the initial capacity to 16, the load factor to 0.75, and the concurrency level to 16. It does not meet the specified requirements. * D. var concurrentHashMap = new ConcurrentHashMap(42, 10);: This constructor sets the initial capacity to 42 and the load factor to 10, which is incorrect because the load factor should be a float value between 0 and 1. * E. var concurrentHashMap = new ConcurrentHashMap(42, 0.88f, 10);: This correctly sets the initial capacity to 42, the load factor to 0.88, and the concurrency level to 10, meeting all the specified requirements. Therefore, the correct answer is option E.