About Question enthuware.ocajp.i.v8.2.1464 :

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

Moderator: admin

Post Reply
ynyus75
Posts: 2
Joined: Thu Jan 07, 2016 12:55 am
Contact:

About Question enthuware.ocajp.i.v8.2.1464 :

Post by ynyus75 »

This question is confusing and couldn't convey the objective effectively due to typos. e.g I don't understand how to interpret the statement return k-->0?true:false;

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

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by admin »

Can you please tell me what typos did you see so that I can confirm?
Did you read the detailed explanation? It explains exactly how to to interpret the statement that you mentioned. Please let me know which part do you have trouble with so that I can elaborate it further.

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

ynyus75
Posts: 2
Joined: Thu Jan 07, 2016 12:55 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by ynyus75 »

Hi Paul,

Sorry for the delayed response but the statement I quoted "return k-->0?true:false;" has typo ("-->") is not a valid operator ... wait a sec, maybe I am wrong; now I get it; I think it should be read as k-- > 0 ? true : false ;

Looks like k-- is post decrement.

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

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by admin »

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

PeterD
Posts: 4
Joined: Thu Jan 26, 2017 8:45 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by PeterD »

Answer and explanation give: "Replace while(checkIt(k)) with while(checkIt(--k)). It will print 4321." And further: "Observe that the method parameter k in checkIt shadows the instance variable k. Therefore, any changes made to k in checkIt will not affect the instance variable k. For checkIt method to access the instance variable k, you need to do this.k."

Why does diminishing the shadowed variable k in checkIt still output 4321? Why is this not printing 5 5 5 5 5?

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

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by admin »

PeterD wrote:Answer and explanation give: "Replace while(checkIt(k)) with while(checkIt(--k)). It will print 4321." And further: "Observe that the method parameter k in checkIt shadows the instance variable k. Therefore, any changes made to k in checkIt will not affect the instance variable k. For checkIt method to access the instance variable k, you need to do this.k."

Why does diminishing the shadowed variable k in checkIt still output 4321? Why is this not printing 5 5 5 5 5?
Because the instance variable k is first being decremented due to --k part of the call to checkIt(--k). The decremented value is being passed to checkIt method. Now, the method checkIt also decrements k but this k refers to the method parameter and has no impact on the instance variable k. So the instance variable k is not decremented twice but only once per call to checkIt(--k).
If you like our products and services, please help us by posting your review here.

catorarn
Posts: 1
Joined: Wed Jul 05, 2017 3:13 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by catorarn »

I would like to understand why the wrong to replace to return this.k-->0?true:false resolves to 43210. Could you provide an explanation? Where does the instance variable get decremented? When I execute the code and I put a System.out.println(this.k) in the checkIt method before the lambda executes I see 5, 44, 33, 22, 11, 00.

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

Re: About Question enthuware.ocajp.i.v8.2.1464 :

Post by admin »

This instance variable k gets decremented because of "this.k--" contained in the statement, return this.k-->0?true:false;

First, k's value is compared with 0. Based on the comparison, the return value (true or false) is determined. In the first iteration, k is 5, so return value is decided as true.
Next, k's value is decremented. So k becomes 4.
Now, the value that was determined in 1st step is returned by checkIt method i.e. true.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests