Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

OCP 17 Study Guide - Online Test review.

Ranch Foreman
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Last really long and boring post for Jeanne and Scott, about all of the small errors I found on the online tests.
Questions with wrong answer:


Given the following, which can correctly fill in the blank? (Choose all that apply.)
var date =;
var time =;
var dateTime =;
var zoneId = ZoneId.systemDefault();
var zonedDateTime = ZonedDateTime.of(dateTime, zoneId);
Instant instant = ______________________________;

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.

Answer B does not compile, and explanation refers to other code.


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.

Option B has a different signature. If I'm not wrong it should compile overloading the method, not overriding it.


Which statement is true of the following module?

  |-- staff

A. The directory structure shown is a valid module.
B. The directory structure would be a valid module if were added directly underneath zoo/staff.
C. The directory structure would be a valid module if were added directly underneath zoo.
D. The directory structure would be a valid module if were added directly underneath zoo/staff.
E. The directory structure would be a valid module if were added directly underneath zoo.
F. None of these changes would make this directory structure a valid module.

Modules are required to have a file at the root directory of the module. Option D matches this requirement.

Correct answer should be answer E.

Confusing/inaccurate answers and explanations:

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.

That might be the only topic of the book I didn't get very well. The answers are coherent with the explanation on the book, but not really with the ones on that topic.

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.

Line 20 actually compiles as var is used. Lines 22(wrong method) and 23(wrong reference) still not compile, so answer is still E.

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.

Ocean declaration does not compile so it can't inherit anything.

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.

Answer D and E should be both wrong as the result should be unknown. Quoting the study guide :

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.

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 (

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.

The explanation seems to come from another question. A SQLException is thrown because the bind variable is not implemented before executing the query.

When printed, which String gives the same value as this text block?

var pooh = """
  "Oh, bother." -Pooh

Underscore after print.

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.

floor() instead of round()
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

Predicate instead of Function.
Finally an errata from the book from Chapter11 - Formatting Number :

Since NumberFormat is an interface, we need the concrete DecimalFormat class to use it.

NumberFormat is actually an abstract class that provides many factory methods. DecimalFormat extends NumberFormat and inherits all of those methods, and that wouldn't be true if NumberFormat was an interface. NumberFormat formatter = DecimalFormat.getCurrencyInstance(); will compile and work correctly. I found it on the real test.

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic