tb864585.JaSE17SG.c04.14
Given the following, which can correctly fill in the blank? (Choose all that apply.)
var date = LocalDate.now();
var time = LocalTime.now();
var dateTime = LocalDateTime.now();
var zoneId = ZoneId.systemDefault();
var zonedDateTime = ZonedDateTime.of(dateTime, zoneId);
Instant instant = ______________________________;
A. Instant.now()
B. new Instant()
C. date.toInstant()
D. dateTime.toInstant()
E. time.toInstant()
F. zonedDateTime.toInstant()
Option A correctly creates the current instant. Option B correctly converts from seconds to an Instant. Option F is also a proper conversion. Options C, D, and E are incorrect because the source object does not represent a point in time. Without a time zone, Java doesn’t know what moment in time to use for the Instant.
tb864585.JaSE17SG.c09.07
Which of the following method signatures are valid overrides of the hairy() method in the Alpaca class? (Choose all that apply.)
import java.util.*;
public class Alpaca {
public List˂String˃ hairy(List˂String˃ list) { return null; }
}
A.public List˂String˃ hairy(List˂CharSequence˃ list) { return null; }
B.public List˂String˃ hairy(ArrayList˂String˃ list) { return null; }
C.public List˂String˃ hairy(List˂Integer˃ list) { return null; }
D.public List˂CharSequence˃ hairy(List˂String˃ list) { return null; }
E.public Object hairy(List˂String˃ list) { return null; }
F.public ArrayList˂String˃ hairy(List˂String˃ list) { return null; }
A valid override of a method with generic arguments must have a return type that is covariant, with matching generic type parameters. Options D and E are incorrect because the return type is too broad. Additionally, the generic arguments must have the same signature with the same generic types. This eliminates options A and C. The remaining options are correct, making the answer options B and F.
tb864585.JaSE17SG.c12.01
Which statement is true of the following module?
|---zoo
|-- staff
|-- Vet.java
A. The directory structure shown is a valid module.
B. The directory structure would be a valid module if module.java were added directly underneath zoo/staff.
C. The directory structure would be a valid module if module.java were added directly underneath zoo.
D. The directory structure would be a valid module if module-info.java were added directly underneath zoo/staff.
E. The directory structure would be a valid module if module-info.java were added directly underneath zoo.
F. None of these changes would make this directory structure a valid module.
Modules are required to have a module-info.java file at the root directory of the module. Option D matches this requirement.
tb864585.JaSE17SG.pe3.18
Which statements are true about migration? (Choose all that apply.)
A. Bottom-up migration involves automatic modules.
B. Bottom-up migration involves unnamed modules.
C. Bottom-up migration involves both the classpath and module path.
D. Top-down migration involves automatic modules.
E. Top-down migration involves unnamed modules.
F. Top-down migration involves both the classpath and module path.
Top-down migration starts with putting all JARs on the module path as automatic modules, making option D correct. Bottom-up migration starts with leaving all JARs on the classpath as unnamed modules, making options B and C correct.
How many lines in this code fail to compile?
A. Zero
B. One
C. Two
D. Three
E. Four
F. Five
Lines 16 and 19 do not compile because they are not allowed in a try-with-resources declaration. Only declarations that implement AutoCloseable or Closeable are permitted. Line 17 does not compile because both the ResultSet type and concurrency mode are required if either is specified. Line 20 does not compile because execute() returns a boolean rather than a ResultSet. Finally, line 22 does not compile because the ResultSet method should be next(), not hasNext(). Since there are five errors, option F is correct.
tb864585.JaSE17SG.pe2.27
Which statements about the following declarations are correct? (Choose all that apply.)
A. The HasWater declaration compiles.
B. The IsSalty declaration compiles.
C. The IsSea declaration does not compile because an interface can extend only one interface.
D. The IsSea declaration compiles because it is not required to implement any of the methods it inherits.
E. The Ocean declaration compiles.
F. The variable color is inherited by the Ocean class.
The HasWater and IsSalty interfaces compile, making options A and B correct. Option C is incorrect, as an interface can extend multiple interfaces. That said, the IsSalty interface still does not compile, but the reason is that the return types of Boolean and boolean are not covariant; in other words, one is not a subtype of the other. For this reason, option D is also incorrect. Even though IsSalty is an abstract type, the compiler can detect that no concrete method could ever override both inherited abstract methods simultaneously. Option E is incorrect, as a class cannot be marked both abstract and final. Finally, option F is correct, as interface variables are implicitly public and inherited in the classes that implement the interface.
tb864585.JaSE17SG.c15.21
Suppose conn is a valid connection object and the exhibits table is empty. Which are true? (Choose two.)
A. As written, the table will remain empty after this code.
B. As written, the table will contain one row after this code.
C. As written, the code will throw an exception.
D. When line W is commented out, the table will remain empty after this code.
E. When line W is commented out, the table will contain one row after this code.
F. When line W is commented out, the code will throw an exception.
The code starts with autocommit off. As written, we turn autocommit mode back on and immediately commit the transaction. This is option B. When line W is commented out, the update gets lost, making option D the other answer.
The other edge case is what happens if you have autocommit set to false and close your connection without rolling back or committing your changes. The answer is that the behavior is undefined. It may commit or roll back, depending solely on the driver.
tb864585.JaSE17SG.c15.09
Suppose that the table enrichment has three rows with the animals bat, rat, and snake. How many lines does this code print?
var sql = "SELECT toy FROM enrichment WHERE animal = ?";
try (var ps = conn.prepareStatement(sql)) {
try (var rs = ps.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
A. 0
B. 1
C. 3
D. The code does not compile.
E. A SQLException is thrown.
F. A different exception is thrown.
The code compiles because PreparedStatement extends Statement, and Statement allows passing a String in the executeQuery() call. While PreparedStatement can have bind variables, Statement cannot. Since this code uses executeQuery(sql) in Statement, it fails at runtime. A SQLException is thrown, making option E correct.
tb864585.JaSE17SG.at.20
When printed, which String gives the same value as this text block?
var pooh = """
"Oh, bother." -Pooh
""".indent(1);
System.out.print_(pooh);
tb864585.JaSE17SG.pe2.12
How many of these lines contain a compiler error? (Choose all that apply.)
12: int one = Math.round(6.6);
13: int two = Math.round(6.6f);
14: long three = Math.round(6.6);
15: long four = Math.round(6.6f);
A. 0
B. 1
C. 2
D. 3
E. 4
The floor() method returns an int when passed a float, which allows lines 13 and 15 to compile. In contrast, it returns a long when passed a double. This allows line 14 to compile, but not line 12. Since one line does not compile, option B is the answer.
tb864585.JaSE17SG.at.23
Which of the following lines can fill in the blank to print true? (Choose all that apply.)
10: public static void main(String[] args) {
11: System.out.println(test(____________________________));
12: }
13: private static boolean test(Function˂Integer, Boolean˃ b) {
14: return b.apply(5);
15: }
A. i::equals(5)
B. i -˃ {i == 5;}
C. (i) -˃ i == 5
D. (int i) -˃ i == 5
E. (int i) -˃ {return i == 5;}
F. (i) -˃ {return i == 5;}
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
Since NumberFormat is an interface, we need the concrete DecimalFormat class to use it.
Suppose that the table counts has five rows with the numbers 1 to 5. How many lines does this code print?
This code is tricky. While it does have two loops for the ResultSet, there is no call to the database in between. Therefore, the second call to setInt() is unused, and the second while loop does not iterate through any data. Only two lines are printed, making option C correct.
Jeanne Boyarsky wrote:
Marco Olivi wrote:1)Why do that code throws an EOFException?
And this one not?
I don't know. Interesting find.
Which statement is true of the following module?
|---zoo
|-- staff
|-- Vet.java
A. The directory structure shown is a valid module.
B. The directory structure would be a valid module if module.java were added directly underneath zoo/staff.
C. The directory structure would be a valid module if module.java were added directly underneath zoo.
D. The directory structure would be a valid module if module-info.java were added directly underneath zoo/staff.
E. The directory structure would be a valid module if module-info.java were added directly underneath zoo.
F. None of these changes would make this directory structure a valid module.
Modules are required to have a module-info.java file at the root directory of the module. Option D matches this requirement.
Identify true statement(s) about migration. Select one option
1) In bottom-up migration all modules are moved to module-path. (correct answer)
2) In top-up migration all modules are moved to module path.
3) In top-up migration, the required modules are migrated first.
4) Unnamed modules become automatic modules in top-down migration.
5) In both the approaches for migration, classpath can be used.
In top-up migration you start with the packages that you are most interested in and upgrade them as named modules, without worrying about their dependencies. You put the dependencies either on module-path (to make them automatic modules) or on classpath (to make them part of the unnamed module).
In top-down migration, classes that cannot be migrated or cannot be upgraded to automatic modules are left on the classpath and they become part of the unnamed module.
I just logged this on the errata from another thread.