Given the code fragments:
Which is true?
B
Explanation:
The code creates two threads, t1 and t2, and starts them. The threads will print their names and the
value of the Atomic Integer object, x, which is initially set to 1. The threads will then increment the
value of x and print their names and the new value of x. Since the threads are started at the same
time, the output will be in random order. However, the final output will always be t1 : 1 : t2: 1 : t1 : 2 :
t2: 2: Reference:
AtomicInteger (Java SE 17 & JDK 17) - Oracle
Which statement is true?
C
Explanation:
A thread in waiting state is waiting for another thread to perform a particular action, such as calling
notify() or notifyAll() on a shared object, or terminating a joined thread. A thread in waiting state can
be interrupted by another thread, which will cause the waiting thread to throw an
InterruptedException and return to the runnable state. Therefore, a thread in waiting state must
handle InterruptedException, either by catching it or declaring it in the throws
clause. Reference:
Thread.State (Java SE 17 & JDK 17)
, [Thread (Java SE 17 & JDK 17)]
Given the code fragment:
Which code fragment invokes all callable objects in the workers set?
A)
B)
C)
D)
C
Explanation:
The code fragment in Option C invokes all callable objects in the workers set by using the
ExecutorService’s invokeAll() method. This method takes a collection of Callable objects and returns
a list of Future objects representing the results of the tasks. The other options are incorrect because
they either use the wrong method (invokeAny() or submit()) or have syntax errors (missing
parentheses or semicolons). Reference:
AbstractExecutorService (Java SE 17 & JDK 17) - Oracle
Given the code fragment:
What is the result?
C
Explanation:
The code fragment is creating a string variable “a” with the value “Hello! Java”. Then, it is printing the
index of “Java” in “a”. Next, it is replacing “Hello!” with “Welcome!” in “a”. Then, it is printing the
index of “Java” in “a”. Finally, it is creating a new StringBuilder object “b” with the value of “a” and
printing the index of “Java” in “b”. The output will be 8109 because the index of “Java” in “a” is 8, the
index of “Java” in “a” after replacing “Hello!” with “Welcome!” is 10, and the index of “Java” in “b” is
9. Reference: Oracle Java SE 17 Developer source and documents: [String (Java SE 17 & JDK 17)],
[StringBuilder (Java SE 17 & JDK 17)]
Given the code fragment:
D
Explanation:
The code fragment compares four pairs of strings using the equals() and intern() methods. The
equals() method compares the content of two strings, while the intern() method returns a canonical
representation of a string, which means that it returns a reference to an existing string with the same
content in the string pool. The string pool is a memory area where strings are stored and reused to
save space and improve performance. The results of the comparisons are as follows:
s1.equals(s2): This returns true because both s1 and s2 have the same content, “Hello Java 17”.
s1 == s2: This returns false because s1 and s2 are different objects with different references, even
though they have the same content. The == operator compares the references of two objects, not
their content.
s1.intern() == s2.intern(): This returns true because both s1.intern() and s2.intern() return a reference
to the same string object in the string pool, which has the content “Hello Java 17”. The intern()
method ensures that there is only one copy of each distinct string value in the string pool.
“Hello Java 17” == s2: This returns false because “Hello Java 17” is a string literal, which is
automatically interned and stored in the string pool, while s2 is a string object created with the new
operator, which is not interned by default and stored in the heap. Therefore, they have different
references and are not equal using the == operator.
Reference:
String (Java SE 17 & JDK 17) - Oracle
Given the code fragment:
Which two statements at Line nl independently enable you to print 1250?
AE
Explanation:
The code fragment is creating a string variable “s” with the value “10_00” and an integer variable
“s2” with the value 10.
The string “s” is using an underscore as a separator for readability, which is
allowed in Java SE 171
. The question is asking for two statements that can add 250 to the numeric
value of “s” and assign it to an integer variable “res”. The correct answers are A and E because they
use the methods parseInt and valueOf of the Integer class to convert the string “s” to an integer.
Both
methods interpret the string as a signed decimal integer and return the equivalent int or Integer
value23
. The other options are incorrect because they either use invalid syntax, such as B and C, or
they do not convert the string “s” to an integer, such as D and F. Reference:
Binary Literals (The Java™
Tutorials > Learning the Java Language > Numbers and Strings)
,
Integer (Java SE 17 & JDK 17)
,
Integer
(Java SE 17 & JDK 17)
Given the code fragment:
What is the result?
D
Explanation:
The code fragment is comparing the values of a, b, and c using the < and > operators. The first
comparison, d, is checking if a is less than b and greater than c. Since a is equal to 2, b is equal to -2,
and c is equal to -4, this comparison will evaluate to true. The second comparison, e, is checking if a
is greater than b and a is greater than c. Since a is equal to 2, b is equal to -2, and c is equal to -4, this
comparison will evaluate to false. Therefore, the result will be true 1 false 2. Reference:
Operators
(The Java™ Tutorials > Learning the Java Language - Oracle
Given:
What is the result?
E
Explanation:
The code will not compile because the variable ‘x’ is declared as final and then it is being modified in
the switch statement. This is not allowed in Java.
A final variable is a variable whose value cannot be
changed once it is initialized1
. The switch statement tries to assign different values to ‘x’ depending
on the value of ‘y’, which violates the final modifier. The compiler will report an error: The final local
variable x cannot be assigned. It must be blank and not using a compound
assignment. Reference:
The final Keyword (The Java™ Tutorials > Learning the Java Language >
Classes and Objects)
Given the code fragment:
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 rank and executes the corresponding case statement. In this case,
the value of rank is 4, so the first case statement is executed, printing “Range1”. The second and third
case statements are also executed, printing “Range2” and “Range3”. The default case statement is
also executed, printing “Not a valid rank”. Reference:
Java Language Changes - Oracle Help Center
Given the code fragment:
What is the result?
A
Explanation:
The code fragment is using the stream methods allMatch, anyMatch, noneMatch, and findFirst on a
list of strings called specialDays.
These methods are used to perform matching operations on the
elements of a stream, such as checking if all, any, or none of the elements satisfy a given predicate,
or finding the first element that matches a predicate1
. The predicate in this case is that the string
equals “Labour” or “Halloween”. The output will be:
False: because not all of the elements in specialDays are equal to “Labour” or “Halloween”.
true: because at least one of the elements in specialDays is equal to “Labour” or “Halloween”.
true: because none of the elements in specialDays are equal to both “Labour” and “Halloween”.
Optional[NewYear]: because the first element in specialDays that matches the predicate is
“NewYear”, and the findFirst method returns an Optional object that may or may not contain a non-
null value2
.
Reference:
Stream (Java SE 17 & JDK 17)
,
Optional (Java SE 17 & JDK 17)
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