Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 56

Answers to Assessment Test

Оглавление

1 G. The question does not compile because line 44 and line 47 do not always return a value in the case block, which is required when a switch expression is used in an assignment operation. Line 44 is missing a yield statement when the if statement evaluates to false, while line 47 is missing a yield statement entirely. Since two lines don't compile, option G is the answer. For more information, see Chapter 3.

2 B. Initially, moon is assigned a value of 9, while star is assigned a value of 8. The multiplication operator (*) has a higher order of precedence than the addition operator (+), so it gets evaluated first. Since star is not greater than 10, sun is assigned a value of 3, which is promoted to 3.0f as part of the assignment. The value of jupiter is (3.0f + 9) - 1.0, which is 11.0f. This value is implicitly promoted to double when it is assigned. In the last assignment, moon is decremented from 9 to 8, with the value of the expression returned as 8. Since 8 less than or equal to 8 is true, mars is set to a value of 2. The final output is 3.0, 11.0, 2, making option B the correct answer. Note that while Java outputs the decimal for both float and double values, it does not output the f for float values. For more information, see Chapter 2.

3 B, C, E. The code may print 100 without any changes, but since the data class is not thread-safe, this behavior is not guaranteed. For this reason, option F is incorrect. Option A is also incorrect, as volatile does not guarantee thread-safety. Options B and C are correct, as they both cause the stream to apply the add() operation in a synchronized manner. Option D is incorrect, as serial() is not a stream method. Option E is correct. Synchronization will cause each thread to wait so that the List is modified one element at a time. For more information, see Chapter 13.

4 D. First, this mess of code does compile. However, the source is an infinite stream. The filter operation will check each element in turn to see whether any are not empty. While nothing passes the filter, the code does not terminate. Therefore, option D is correct. For more information, see Chapter 10.

5 B. The code compiles successfully, so options D and E are incorrect. The value of a cannot be changed by the addToInt() method, no matter what the method does, because only a copy of the variable is passed into the parameter x. Therefore, a does not change, and the output on line 9 is 15 which is option B. For more information, see Chapter 5.

6 C. Java will use Penguin_en.properties as the matching resource bundle on line 7. Since there is no match for French, the default locale is used. Line 8 finds a matching key in the Penguin_en.properties file. Line 9 does not find a match in the Penguin_en.properties file; therefore, it has to look higher up in the hierarchy to Penguin.properties. This makes option C the answer. For more information, see Chapter 11.

7 D, E. The array is allowed to use an anonymous initializer because it is in the same line as the declaration. The results of the binary search are undefined since the array is not sorted. Since the question asks about guaranteed output, options A and B are incorrect. Option D is correct because the compare() method returns 0 when the arrays are the same length and have the same elements. Option E is correct because the mismatch() method returns a -1 when the arrays are equivalent. For more information, see Chapter 4.

8 C, E, F. First, note that option A is incorrect because the interface should be BiPredicate and not BinaryPredicate. Line 6 requires you to know that negate() is a convenience method on Predicate. This makes option E correct. Line 7 takes zero parameters and doesn't return anything, making it a Runnable. Remember that Runnable doesn't use generics. This makes option F correct. Finally, line 8 takes two parameters and returns an int. Option C is correct. Comparable is there to mislead you since it takes only one parameter in its single abstract method. For more information, see Chapter 8.

9 D. If this were a valid module-info.java file, it would need to be placed at the root directory of the module, which is option A. However, a module is not allowed to use the public access modifier. Option D is correct because the provided file does not compile regardless of placement in the project. For more information, see Chapter 12.

10 C. The getTailLength() method in the interface is private; therefore, it must include a body. For this reason, line 1 is the only line that does not compile and option C is correct. Line 3 uses a different return type for the method, but since it is private in the interface, it is not considered an override. Note that line 7 defines an anonymous class using the abstract Puma parent class. For more information, see Chapter 7.

11 C, E, F. The jump() method has package access, which means it can be accessed only from the same package. Tadpole is not in the same package as Frog, causing lines 7 and 10 to trigger compiler errors and giving us options C and F. The ribbit() method has protected access, which means it can only be accessed from a subclass reference or in the same package. Line 6 is fine because Tadpole is a subclass. Line 9 does not compile and our final answer is option E because the variable reference is to a Frog, which doesn't grant access to the protected method. For more information, see Chapter 5.

12 B. DataSource isn't on the exam, so any question containing one is wrong. The key variables used in running a query are Connection, PreparedStatement, and ResultSet. A Connection is obtained through a DriverManager, making option B correct. For more information, see Chapter 15.

13 C, D. The mySet declaration defines an upper bound of type RuntimeException. This means that classes may specify RuntimeException or any subclass of RuntimeException as the type parameter. Option B is incorrect because Exception is a superclass, not a subclass, of RuntimeException. Option A is incorrect because the wildcard cannot occur on the right side of the assignment. Options C and D compile and are the answers. For more information, see Chapter 9.

14 D, E. Line 10 includes an unhandled checked IOException, while line 11 includes an unhandled checked FileNotFoundException, making option D correct. Line 12 does not compile because is.readObject() must be cast to a Bird object to be assigned to b. It also does not compile because it includes two unhandled checked exceptions, IOException and ClassNotFoundException, making option E correct. If a cast operation were added on line 12 and the main() method were updated on line 8 to declare the various checked exceptions, the code would compile but throw an exception at runtime since Bird does not implement Serializable. Finally, if the class did implement Serializable, the program would print null at runtime, as that is the default value for the transient field age. For more information, see Chapter 14.

15 C. Option A is incorrect because var is only allowed as a type for local variables, not instance members. Options B and E are incorrect because new and case are reserved words and cannot be used as identifiers. Option C is correct, as var can be used as a method name. Option D is incorrect because a single underscore (_) cannot be used as an identifier. Finally, option F is incorrect because var cannot be specified as the return type of a method. For more information, see Chapter 1.

16 A, D. This code is correct, eliminating options E and F. JDBC will use the existing parameter set if you don't replace it. This means Kara's row will be set to use NY as the third parameter. Rolling back to a savepoint throws out any changes made since. This leaves Joslyn and eliminates Kara, making option D correct. Rolling back without a savepoint brings us back to the beginning of the transaction, which is option A. For more information, see Chapter 15.

17 C, F. Option C is correct as mismatch() throws an exception if the files do not exist unless they both refer to the same file. Additionally, option F is correct because the first index that differs is returned, which is the second character. Since Java uses zero-based indexes, this is 1. For more information, see Chapter 14.

18 F. The Amphibian class is marked final, which means line 3 triggers a compiler error and option F is correct. For more information, see Chapter 6.

19 C. The code compiles and runs without issue; therefore, options E and F are incorrect. This type of problem is best examined one loop iteration at a time:On the first iteration of the outer loop, i is 0, so the loop continues.On the first iteration of the inner loop, i is updated to 1 and x to 6. The if statement branch is not executed, and x is increased to 10 and j to 1.On the second iteration of the inner loop (since j = 1 and 1 <= 2), i is updated to 2 and x to 11. At this point, the if branch will evaluate to true for the remainder of the program run, which causes the flow to break out of the inner loop each time it is reached.On the second iteration of the outer loop (since i = 2), i is updated to 3 and x to 12. As before, the inner loop is broken since x is still greater than 10.On the third iteration of the outer loop, the outer loop is broken, as i is already not less than 3. The most recent value of x, 12, is output, so the answer is option C.For more information, see Chapter 3.

20 C. First, note that the text block has the closing """ on a separate line, which means there is a new line at the end and rules out options D, E, and F. Additionally, text blocks don't start with a new line, ruling out options A and B. Therefore, option C is correct. For more information, see Chapter 1.

21 C. Only named modules are required to have a module-info.java file, ruling out options A, B, E, and F. Unnamed modules are not readable by any other types of modules, ruling out option D. Automatic modules always export all packages to other modules, making the answer option C. For more information, see Chapter 12.

22 E. When the same key is put into a Map, it overrides the original value. This means that line 23 could be omitted and the code would be the same, and there are only three key/value pairs in the map. TreeMap sorts its keys, making the order M followed by k followed by m. Remember that natural sort ordering has uppercase before lowercase. The replaceAll() method runs against each element in the map, doubling the value. Finally, we iterate through each key, printing 846 and making option E correct. For more information, see Chapter 9.

23 C, F. Option A looks like a method reference. However, it doesn't call a valid method, nor can method references take parameters. The Predicate interface takes a single parameter and returns a boolean. Lambda expressions with one parameter are allowed to omit the parentheses around the parameter list, making option C correct. The return statement is optional when a single statement is in the body, making option F correct. Option B is incorrect because a return statement must be used if braces are included around the body. Options D and E are incorrect because the type is Integer in the predicate and int in the lambda. Autoboxing works for collections, not inferring predicates. If these two were changed to Integer, they would be correct. For more information, see Chapter 8.

24 D. String literals are used from the string pool. This means that s1 and s2 refer to the same object and are equal. Therefore, the first two print statements print true. While the indent() and strip() methods create new String objects and the third statement prints false, the intern() method reverts the String to the one from the string pool. Therefore, the fourth print statement prints true. The fifth print statement prints false because toString() uses a method to compute the value, and it is not from the string pool. The final print statement again prints true because equals() looks at the values of String objects. Since four are true, option D is the answer. For more information, see Chapter 4.

25 C. The Reindeer object is instantiated using the constructor that takes an int value. Since there is no explicit call to the parent constructor, the compiler inserts super() as the first line of the constructor on line 7. The parent constructor is called, and Deer is printed on line 2. The flow returns to the constructor on line 7, with Reindeer being printed. Next, the hasHorns() method is called. The reference type is Deer, and the underlying object type is Reindeer. Since Reindeer correctly overrides the hasHorns() method, the version in Reindeer is called, with line 11 printing ,true. Therefore, option C is correct. For more information, see Chapter 6.

26 B, F. Calling get() on an empty Optional causes an exception to be thrown, making option B correct. Option F is also correct because filter() makes the Optional empty before it calls get(). Option C is incorrect because the infinite stream is made finite by the intermediate limit() operation. Options A and E are incorrect because the source streams are not infinite. Therefore, the call to max() sees only three elements and terminates. For more information, see Chapter 10.

27 C. Music does not compile because records cannot include instance variables not listed in the declaration of the record, as it could break immutability. Song does not compile because a compact constructor cannot set an instance variable. The record would compile if this were removed from the compact constructor, as compact constructors can modify input parameters. March does not compile because it is an invalid override; it reduces the visibility of the toString() method from public to package access. Ballet does not compile because the subclass of a sealed class must be marked final, sealed, or non-sealed. Since the only one that compiles is Dance, option C is the answer. For more information, see Chapter 7.

28 B, D. Option A does not compile, as the expression 3 + 2.0 is evaluated as a double, and a double requires an explicit cast to be assigned to an int. Option B compiles without issue, as a long value can be implicitly cast to a double. Option C does not compile because the ternary operator (? :) is missing a colon (:), followed by a second expression. Option D is correct. Even though the int value is larger than a short, it is explicitly cast to a short, which means the value will wrap around to fit in a short. Option E is incorrect, as you cannot use a decimal (.) with the long (L) postfix. Finally, option F is incorrect, as an underscore cannot be used next to a decimal point. For more information, see Chapter 2.

29 F. The code compiles without issue. The key to understanding this code is to notice that our thread executor contains only one thread, but our CyclicBarrier limit is 3. Even though 12 tasks are all successfully submitted to the service, the first task will block forever on the call to await(). Since the barrier is never reached, nothing is printed, and the program hangs, making option F correct. For more information, see Chapter 13.

30 F. Line 5 does not compile as the FileNotFoundException thrown on line 12 is not handled or declared by the method. Line 7 does not compile because StringBuilder does not implement AutoCloseable and is therefore not compatible with a try-with-resource statement. Finally, line 10 does not compile as RuntimeException is a subclass of Exception in the multi-catch block, making it redundant. Since this method contains three compiler errors, option F is the correct answer. For more information, see Chapter 11.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх