OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky. Читать онлайн. Newlib. NEWLIB.NET

Автор: Jeanne Boyarsky
Издательство: John Wiley & Sons Limited
Серия:
Жанр произведения: Программы
Год издания: 0
isbn: 9781119864592
Скачать книгу
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.