Given:
and the code fragment:
A
Explanation:
The code fragment is using the Stream API to perform a reduction operation on a list of
ElectricProduct objects. The reduction operation consists of three parts: an identity value, an
accumulator function, and a combiner function. The identity value is the initial value of the result,
which is 0.0 in this case. The accumulator function is a BiFunction that takes two arguments: the
current result and the current element of the stream, and returns a new result. In this case, the
accumulator function is (a,b) -> a + b.getPrice (), which means that it adds the price of each element
to the current result. The combiner function is a BinaryOperator that takes two partial results and
combines them into one. In this case, the combiner function is (a,b) -> a + b, which means that it
adds the two partial results together.
The code fragment then applies a filter operation on the stream, which returns a new stream that
contains only the elements that match the given predicate. The predicate is p -> p.getPrice () > 10,
which means that it selects only the elements that have a price greater than 10. The code fragment
then applies a map operation on the filtered stream, which returns a new stream that contains the
results of applying the given function to each element. The function is p -> p.getName (), which
means that it returns the name of each element.
The code fragment then calls the collect method on the mapped stream, which performs a mutable
reduction operation on the elements of the stream using a Collector. The Collector is
Collectors.joining (“,”), which means that it concatenates the elements of the stream into a single
String, separated by commas.
The code fragment then prints out the result of the reduction operation and the result of the collect
operation, separated by a new line. The result of the reduction operation is 300.00, which is the sum
of the prices of all ElectricProduct objects that have a price greater than 10. The result of the collect
operation is CellPhone,ToyCar,Motor,Fan, which is the concatenation of the names of all
ElectricProduct objects that have a price greater than 10.
Therefore, the output of the code fragment is:
300.00 CellPhone,ToyCar,Motor,Fan
Reference:
Stream (Java SE 17 & JDK 17) - Oracle
,
Collectors (Java SE 17 & JDK 17) - Oracle
Given the code fragment:
Which action sorts the book list?
D
Explanation:
The code fragment is trying to sort a list of books using the Collections.sort() method. The correct
answer is D, because the compareTo() method is not the correct way to compare two objects in a
Comparator.
The compare() method is the correct way to compare two objects in a Comparator and
return an int value that indicates their order1
.
The compareTo() method is used to implement the
Comparable interface, which defines the natural order of objects of a class2
.
The other options are
incorrect because they either do not change the type of the list, which is already mutable, or they do
not use the correct syntax for sorting a stream, which requires a terminal operation such as
collect()3
. Reference:
Comparator (Java SE 17 & JDK 17)
,
Comparable (Java SE 17 & JDK 17)
,
Stream
(Java SE 17 & JDK 17)
Given the code fragment:
What is the result?
B
Explanation:
The code fragment uses the Collections.binarySearch method to search for the string “e3” in the list.
The first search returns the index of the element, which is 2. The second search returns the index of
the element, which is 0. The third search returns the index of the element, which is -4. The final
result is 2. Reference:
Collections (Java SE 17 & JDK 17) - Oracle
Given:
What is the result?
E
Explanation:
The code snippet is an example of Java SE 17 code. The code is checking if the object is an instance of
class C and if it is, it will print “mC”. If it is not an instance of class C, it will print “mA”. In this case,
the object is not an instance of class C, so the output will be “mA”. Reference:
Pattern Matching for
instanceof - Oracle Help Center
Given the directory structure:
Given the definition of the Doc class:
Which two are valid definition of the wordDoc class?
AF
Explanation:
The correct answer is A and F because the wordDoc class must be a non-sealed class or a final class to
extend the sealed Doc class. Option B is incorrect because the wordDoc class must be non-sealed or
final. Option C is incorrect because the wordDoc class cannot be in a different package than the Doc
class. Option D is incorrect because the wordDoc class cannot be a sealed class. Option E is incorrect
because the wordDoc class cannot be an abstract class. Reference:
Oracle Certified Professional: Java
SE 17 Developer
,
3 Sealed Classes - Oracle Help Center
Given:
What is the result
C
Explanation:
The code fragment is using the switch statement with the new Java 17 syntax. The switch statement
checks the value of the variable desig and executes the corresponding case statement. In this case,
the value of desig is “CTO”, which does not match any of the case labels. Therefore, the default case
statement is executed, which prints “Undefined”. The other case statements are not executed,
because there is no fall through in the new syntax. Therefore, the output of the code fragment is:
Undefined
Given:
What is the result?
E
Explanation:
The code is defining an enum class called Forecast with three values: SUNNY, CLOUDY, and RAINY. The
toString() method is overridden to always return “SNOWY”. In the main method, the ordinal value of
SUNNY is printed, which is 0, followed by the value of CLOUDY converted to uppercase, which is
“CLOUDY”. However, since the toString() method of Forecast returns “SNOWY” regardless of the
actual value, the output will be “0 SNOWY”. Reference:
Enum (Java SE 17 & JDK 17)
,
Enum.EnumDesc
(Java SE 17 & JDK 17)
Given the product class:
And the shop class:
What is the result?
E
Explanation:
The code fragment will fail to compile because the readObject method in the Product class is missing
the @Override annotation. The readObject method is a special method that is used to customize the
deserialization process of an object. It must be declared as private, have no return type, and take a
single parameter of type ObjectInputStream. It must also be annotated with @Override to indicate
that it overrides the default behavior of the ObjectInputStream class. Without the @Override
annotation, the compiler will treat the readObject method as a normal method and not as a
deserialization hook. Therefore, the code fragment will produce a compilation
error. Reference:
Object Serialization - Oracle
, [ObjectInputStream (Java SE 17 & JDK 17) - Oracle]
Given:
What is the result?
B
Explanation:
The answer is B because the code uses the writeObject and readObject methods of
the ObjectOutputStream and ObjectInputStream classes to serialize and deserialize the Game object.
These methods use the default serialization mechanism, which writes and reads the state of the
object’s fields, including the inherited ones. Therefore, the title field of the Software class is also
serialized and deserialized along with the players field of the Game class. The toString method of
the Game class calls the toString method of the Software class using super.toString(), which returns
the value of the title field. Hence, when the deserialized object is printed, it shows “Software Game
Software Game Chess 2”. Reference:
Oracle Certified Professional: Java SE 17 Developer
Java SE 17 Developer
OCP Oracle Certified Professional Java SE 17 Developer Study Guide
Serialization and Deserialization in Java with Example
Given the code fragments:
Which action prints Wagon : 200?
F
Explanation:
The code fragment is trying to read an object from a file using the ObjectInputStream class. This class
throws an IOException and a ClassNotFoundException. To handle these exceptions, the main method
signature should declare that it throws these exceptions. Otherwise, the code will not compile. If the
main method throws these exceptions, the code will print Wagon : 200, which is the result of calling
the
toString
method
of
the
LuxuryCar
object
that
was
written
to
the
file. Reference:
ObjectInputStream (Java SE 17 & JDK 17) - Oracle
,
ObjectOutputStream (Java SE 17 &
JDK 17) - Oracle