Assessment Flashcards
What is a possible output of the following?
var trainDay = LocalDate.of(2022, 5, 14); var time = LocalTime.of(10, 0); var zone = ZoneId.of("America/Los_Angeles"); var zdt = ZonedDateTime.of(trainDay, time, zone); var instant = zdt.toInstant(); instant = instant.plus(1, ChronoUnit.YEARS); System.out.println(instant);
While an Instant represents a specific moment in time using GMT, Java only allows adding or removing units of DAYS or smaller. This code throws an UnsupportedTemporalTypeException
because of the attempt to add YEARS.
generic
? extends RuntimeException cannot be assigned a broader type.
any type of generic list can go in List˂?˃
? super RuntimeException does allow a broader exception type.
- You can’t use generics with a primitive
Modules
Which are true of jlink
? (Choose two.)
B. At least the modules specified by –add-modules are included in the runtime image.
E. The output is a directory.
jdeps
E. jdeps -jdkinternals sneaky.jar
F. jdeps –jdk-internals sneaky.jar
Which are true statements about the majority of steps in migrating to a modular application? (Choose two.)
C. In a bottom-up migration, unnamed modules turn into named modules.
D. In a top-down migration, automatic modules turn into named modules.
A fully modular application has all named modules, making options B and E incorrect. A bottom-up migration starts out with unnamed modules, making option C correct. By contrast, a top-down migration starts by making all modules automatic modules, making option D correct.
The service locator contains a ServiceLoader call to the load() method to look up,
The ServiceLoader.load() method needs to be called to get an instance before calling stream().
Unnamed modules are always on the classpath. Both automatic and named modules are on the module path. Named modules always contain a module-info file, but automatic modules never do. Option E is correct, as it meets both these criteria.
The javac command uses -p and –module-path to supply the module path. There are two valid long forms of the classpath option: -classpath and –class-path. Options A and C match these.
What is the output of the following?
var list = Arrays.asList("flower", "seed", "plant"); Collections.sort(list); Collections.reverse(list); var result = Collections.binarySearch(list, list.get(0), Comparator.reverseOrder()); System.out.println(result);
A. 0
First, the list is sorted ascendingly. Then it is reversed so it is in descending order. Since our binary search also uses descending order, we meet the pre-condition for binary search. At this point, the list contains [seed, plant, flower]. The key is to notice that the first element is now seed rather than flower. Calling a binary search to find the position of seed returns 0, which is the index matching that value. Therefore, the answer is option A.
What is the output of the following?
var builder = new StringBuilder("Leaves growing"); do { builder.delete(0, 5); } while (builder.length() ˃ 5); System.out.println(builder);
- On the first iteration through the loop, the first five characters are removed, and builder becomes “s growing”.
- Since there are more than five characters left, the loop iterates again. This time, five more characters are removed, and builder becomes “wing”.
- This matches option C.
What does the following output?
var dice = new LinkedList˂Integer˃(); dice.offer(3); dice.offer(2); dice.offer(4); System.out.print(dice.stream().filter(n -˃ n != 4));
- The code correctly creates a LinkedList with three elements. The stream pipeline does compile.
- However, there is no terminal operation, which means the stream is never evaluated, and the output is something like java.util.stream.ReferencePipeline$2@404b9385.
- This is definitely not one of the listed choices, so option E is correct.
Operator
- bitwise complement by negating the value and subtracting one.
- instanceof
- You are allowed to use null with instanceof; it just prints false.
- Then it gets interesting. We know that bus is not an ArrayList or Collection. However, the compiler only knows that bus is not an ArrayList because ArrayList is a concrete class.
~~~
System.out.println(bus instanceof ArrayList);
~~~
Assuming the current locale uses dollars ($) and the following method is called with a double value of 960_010, which of the following values are printed? (Choose two.)
public void print(double t) { System.out.print(NumberFormat.getCompactNumberInstance().format(t)); System.out.print( NumberFormat.getCompactNumberInstance( Locale.getDefault(), Style.SHORT).format(t)); System.out.print(NumberFormat.getCurrencyInstance().format(t)); }
The code compiles and runs without issue. When a CompactNumberFormat instance is requested without a style, it uses the SHORT style by default. This results in both of the first two statements printing 960K, making option A correct. Option D is also correct, as the full value is printed with a currency formatter without rounding.
JDBC
The ResultSet interface does not have a hasNext() method.
prepareCall() returns a CallableStatement.
stored procedure
What is the result of calling this method?
16: void play() { 17: record Toy(String name){ } 18: 19: var toys = Stream.of( 20: new Toy("Jack in the Box"), 21: new Toy("Slinky"), 22: new Toy("Yo-Yo"), 23: new Toy("Rubik's Cube")); 24: 25: var spliterator = toys.spliterator(); 26: var batch1 = spliterator.trySplit(); 27: var batch2 = spliterator.trySplit(); 28: var batch3 = spliterator.trySplit(); 29: 30: batch1.forEachRemaining(System.out::print); 31: }
Line 25 creates a Spliterator containing four toys. Line 26 splits them into two equal groups by removing the first half of the elements and making them available to batch1. These get printed on line 30, making option E the answer. Note that the splits on lines 27 and 28 are meaningless since batch1 has already been split off.
What is the output of the following application?
abstract class TShirt {
abstract int insulate();
public TShirt() {
System.out.print(“Starting…”);
}
}
public class Wardrobe {
abstract class Sweater extends TShirt {
int insulate() {return 5;}
}
private void dress() {
final class Jacket extends Sweater { // v1
int insulate() {return 10;}
};
final TShirt outfit = new Jacket() { // v2
int insulate() {return 20;}
};
System.out.println(“Insulation:”+outfit.insulate());
}
public static void main(String… snow) {
new Wardrobe().dress();
} }
The code does not compile because the Jacket is marked final and cannot be extended by the anonymous class declared on line v2. Since this line doesn’t compile, option D is correct.
Exception
The second catch block on line p2 does not compile. Since IllegalArgumentException is a subclass of Exception, they cannot be used in the same multi-catch block since it is redundant.
Line 9 is not reachable and does not compile, since FileNotFoundException is a subclass of IOException, which has already been caught. Next, the local variable e is declared twice within the same scope, with the declaration on line 12 failing to compile. Finally, the openDrawbridge() method declares the checked Exception class, but it is not handled or declared in the main() method on line 21. Since lines 9, 12, and 21 need to be changed for the code to compile, option D is correct.
ClassCastException is a runtime exception
Collection
ArrayDeque
Collectors.teeing(Collectors.counting(), Collectors.summingInt(Pet::age))
A teeing() collector takes three parameters.
Concurrency
To perform a concurrent reduction, the stream or the collector must be unordered. Since it is possible to use an ordered collector with an unordered stream and achieve a parallel reduction, option A is incorrect. Option B is also incorrect. While having a thread-safe collection is preferred, it is not required. Stateful lambda expressions should be avoided, whether the stream is serial or parallel, making option C incorrect. Option D is incorrect as there is no class/interface within the JDK called ParallelStream. Options E and F are correct statements about performing parallel reductions.
ExecutorService.submit() that takes a Runnable expression, it returns Future˂?˃, since the return type is void.
The parallelStream() method is found in the Collection interface, not the Stream interface.
enum
enum constructors cannot be public
case statement must use an enum value without the type.
A semicolon is required after a list of enum values if the enum contains anything besides the list of values.