About Question enthuware.ocpjp.v7.2.1313 :

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

Moderator: admin

Post Reply
mcormaechea
Posts: 2
Joined: Sat Jul 26, 2014 10:40 am
Contact:

About Question enthuware.ocpjp.v7.2.1313 :

Post by mcormaechea »

Hi, the line of code  assert enabled = true; is assigning true to enabled, so there will never be any exceptions thrown. I mean, the correct answer "It will not throw anything if assertions are disabled." is wrong, because even if asssertions are enabled, nothing will be thrown. Is it right? Thanks.

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

Re: About Question enthuware.ocpjp.v7.2.1313 :

Post by admin »

Yes, the given code is not useful and that is the point it is trying to get at.

But the statement, "It will not throw anything if assertions are disabled." is true because it states a fact. i.e. if assertions are disabled, no exception will be thrown. The statement doesn't say anything about what happens if assertions are enabled.

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

jbilkes
Posts: 21
Joined: Tue Sep 09, 2014 3:28 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1313 :

Post by jbilkes »

The code in the question:
boolean enabled = false; assert enabled = true; assert enabled; ... rest of the code ...

The answer states that it should be like:
assert enabled = true;
if( !enabled ) throw new RuntimeException("Assertions should be enabled");

but that RuntimeException can still never be thrown since enabled is always false, so while the question is correct, the explanation is far from useful stated this way since it may look like it differs from the code in the question, but that is not the case. The only difference is that "assert enabled;" has been removed, kind of odd to present that as an explanation :)

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

Re: About Question enthuware.ocpjp.v7.2.1313 :

Post by admin »

Why would enabled be always false? If assertions are enabled, the statement assert enabled = true; sets enabled to true and the the next line if( !true) will not throw an RTE.
If the assertions are disabled, the assert enabled = true; will not execute and in that case enabled will remain false. Therefore, if( !false) will cause cause the RTE to be thrown.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 81 guests