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”. References: Pattern Matching for instanceof - Oracle Help Center
Questions # 12:
Given the code fragment:
What is the result?
Options:
A.
Cat
Dog
B.
A NullPointerException is thrown
Cat
Cat
C.
Dog
Dog
D.
Cat
null
Answer
D
Explanation
The answer is E because the code fragment creates a new Pet object with the name “Dog” and assigns it to the variable p. Then, it assigns p to p1. Next, it changes the name of p1 to “Cat”. Then, it assigns p1 to p. Finally, it sets p to null and prints the name of p and p1. The output will be “Cat” and “null” because p is set to null and p1 still points to the Pet object with the name “Cat”.
Questions # 13:
Given the code fragment:
What is the result?
Options:
A.
$SIM6D
B.
PT5000PT60MP6D
C.
PT5SPTIMP6D
D.
5000$60M6D
Answer
B
Explanation
The code fragment is creating a Duration object with a value of 5000 milliseconds, then printing it. Then, it is creating another Duration object with a value of 60 seconds, then printing it. Finally, it is creating a Period object with a value of 6 days, then printing it. The output will be “PT5000PT60MP6D”. References: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html, https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
Questions # 14:
Given:
Options:
A.
Hello
B.
Compilation fails
C.
A NumberFormatException is thrown
D.
there
Answer
B
Explanation
The code fragment will fail to compile because the parseInt method of the Integer class is a static method, which means that it can be invoked without creating an object of the class. However, the code is trying to invoke the parseInt method on an object of type Integer, which is not allowed. The correct way to invoke the parseInt method is by using the class name, such as Integer.parseInt (s). Therefore, the code fragment will produce a compilation error. References: Integer (Java SE 17 & JDK 17) - Oracle
Questions # 15:
Given the code fragment:
Which action sorts the book list?
Options:
A.
At Line n2, replace books,sort() with books.stream().sort(0.
B.
At line n1, convert books type to mutable ArrayList type.
C.
At Line n1, convert type to mutable array type.
D.
At Line n2, replace compareTo () with compare ().
Answer
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. References: Comparator (Java SE 17 & JDK 17), Comparable (Java SE 17 & JDK 17), Stream (Java SE 17 & JDK 17)