Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 24

Given:

It is required that if p instanceof Pair then p.isValid() returns true.

Which is the smallest set of visibility changes to insure this requirement is met?

    Correct Answer: A

    For the requirement that if p is an instance of Pair, then p.isValid() must return true, the visibility changes must ensure that the left and right fields cannot be directly modified from outside the class. The setLeft and setRight methods also should not be accessible from outside because they allow setting left and right without validation. Therefore, making the fields left and right, as well as the methods setLeft and setRight, private ensures that the Pair instance can only be modified through the set method, which enforces validation using the BiFunction validator. This guarantees that isValid() will always return true if p is an instance of Pair.

Discussion
Matt_GOption: A

All methods of changing left and right must be private or a user in the same package could put it into a state where the validator returns false

d7bb0b2Option: A

The option A. "left, right, setLeft, and setRight must be private" is the correct one to ensure that if p is an instance of Pair, then p.isValid() always returns true. This is because if the fields left and right and the methods setLeft and setRight are private, then they cannot be accessed or modified outside of the Pair class. This means that the only way to modify the left and right fields is through the set method, which already checks the biFunction condition before setting the values of left and right. If the biFunction condition is not met, the set method throws an IllegalArgumentException, so it is not possible to create a Pair instance that is not valid.

StavokOption: C

The correct answer is C. left and right must be private. This is because the requirement states that if p is an instance of Pair, then p.isValid() must return true. To ensure this, the left and right fields must be private so that they can only be modified by the setLeft and setRight methods, which in turn call the validator function to check if the new values are valid before setting them. This ensures that the isValid method always returns true.

Lebannin

setLeft and setRight don't really call the validatio though

Mukes877Option: C

minimal changes will be : C. left and right must be private.