About Question enthuware.ocpjp.ii.v11.2.3402 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
FlatPanda
Posts: 8
Joined: Sun Feb 23, 2020 6:51 am
Contact:

About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by FlatPanda »

Based on this question: we only should assume that a question is about immutability, when it is explicitly stated? I know that in order to make this class immutable there is a lot of other things to do, but I still was thrown off-track because of thinking about immutability.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by admin »

Unfortunately, the questions on this topic in the exam are somewhat ambiguous. The right option(s) depend not only on technical correctness but also on the number of correct options that you have to select. You need to select only the best option(s) even if you find another to be also technically correct. For this reason, it is not possible to provide a firm rule that can deduce the correct answer.

In this question, I think "Make the class final" should also be a correct option.

Paul.
If you like our products and services, please help us by posting your review here.

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by kabanvau »

Validation of dob must be done after cloning. Am I right?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by admin »

Yes, you are right. validation should be done after cloning.
If you like our products and services, please help us by posting your review here.

olsongrant
Posts: 8
Joined: Mon Nov 02, 2020 10:21 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by olsongrant »

My experience with this question was that it just said "Given <this code>", and then it provided answer choices that were in the form of "should" statements.

The answers considered correct are that (1) the protected modifier on validateDob should be changed to private, to prevent subclasses from messing with the validation behavior, and (2) the class should be made final. When I looked at these two in conjunction, I perceived the privatization of the validateDob method to be redundant with the making of the class final (from a don't-mess-with-the-behavior perspective), so I figured that those two choices wouldn't together form a coherent answer. (Maybe I still need to learn something about what it means for a class to be final?) Meanwhile, I don't know that we can automatically assume that Person should be an immutable, final class. If I were building a system that involved a Person class, I'd be tempted to make subtypes for different roles.
I don't want to assume I know the intent of the question, but my purpose for writing is just to suggest that there might be some improvements to make by clarifying the context and intended usage of the Person class.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by admin »

Yes, your argument is correct. The problem statement is missing the statement:
"What changes should be made to make this class immutable?"

Regarding making the class final, again, you are right. There are several possibilities in real life, but for the purpose of the exam, we need to pick the options based on Java Security guidelines. So, based on that, option 2 and 3 are valid.
If you like our products and services, please help us by posting your review here.

2014098616
Posts: 5
Joined: Thu Oct 08, 2020 7:23 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by 2014098616 »

Hello Admin, I also think making the validateDob method final still protect the creation of the object as subclasses will not be able to override the method, moreover, the method itself, even if subclasses were allowed to invoke it, does not seem to interfere with the object state. I also agree that making the method private solves the same problem as the subclasses do not see the method.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by admin »

1. validateDOB should be private because it is being invoked from a constructor.
2. The class should be made final because the code shows " //other methods not shown" and it is recommended for an immutable class to be final.

So, in our view, for the purpose of this exam, if you have to select 2 options, both the options should be selected.
If you like our products and services, please help us by posting your review here.

philippe
Posts: 29
Joined: Sun Jul 16, 2017 4:24 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by philippe »

In the following security guideline: https://www.oracle.com/java/technologie ... e.html#6-1
it is written that:
Immutable classes themselves should declare fields final.
Why is option 4:
name and dob fields should be made final.
not a correct option?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by admin »

Yes, ideally the fields should be final as well. Unfortunately, the questions on this topic tend to be somewhat ambiguous because the code given in the question is either not complete or is not realistic. So, the same thing can be achieve with different approaches.
For example, in this question, the focus is on invoking a non-final non-private method from a constructor. Since no other code is shown to modify the fields, the class is still immutable if the fields are not final, which, of course, violates guideline 6.1

So, the point is that, answers to such questions may be debated forever. It depends on what the question writer at Oracle thinks is the right answer. It is best to use your judgement based on the number of correct options that you have to select.
If you like our products and services, please help us by posting your review here.

brintal
Posts: 6
Joined: Sun Jan 23, 2022 8:43 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.3402 :

Post by brintal »

I'm also on the "Option 4 should be correct"-Team.

Because Option 3 "Person class should be made final." is enough to prevent subclasses that might override validateDob.
Hence Option 2 is unnecessary (because overriding validateDob is not possible anyways) and Option 4 makes much more sense in combination with Option 3.

Post Reply

Who is online

Users browsing this forum: No registered users and 30 guests