About Question enthuware.ocpjp.v17.2.3486 :

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

Moderator: admin

Post Reply
edufin166@yahoo.com
Posts: 24
Joined: Wed Sep 28, 2022 9:41 am
Contact:

About Question enthuware.ocpjp.v17.2.3486 :

Post by edufin166@yahoo.com »

This questions has problems? Are sure those alternatives are ok?

edufin166@yahoo.com
Posts: 24
Joined: Wed Sep 28, 2022 9:41 am
Contact:

Re: About Question enthuware.ocpjp.v17.2.3486 :

Post by edufin166@yahoo.com »

How the method getH() can be executed in the instantiation of Baap b = new Beta(); or Beta bb = (Beta) b;, if it is not in the NoARgs-Constructor?
It is strange.....
How Can the answer be:
Beta 44 4 44 Beta 44 44 44
???

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

Re: About Question enthuware.ocpjp.v17.2.3486 :

Post by admin »

Did you try compiling and running the given code? There is a detailed explanation also.

learningtocode
Posts: 1
Joined: Tue Jun 04, 2024 4:35 am
Contact:

Re: About Question enthuware.ocpjp.v17.2.3486 :

Post by learningtocode »

Hi, how is the answer Beta 44 4 44 Beta 44 44 44
and not
4 Beta 44 44 44 Beta 44 44?

I am not understanding why the number is not the first part of the result rather than the word as the system.out method calls b.h/b.hh first = 4/44, then the method b.getH()/bb.getH() = Beta 44 44 / Beta 44 44.

I have run it on my compiler and have achieved the same result as in here, however run on another compiler I have achieved the result I excepted which 4 Beta 44 44 44 Beta 44 44.

I have even played around with the code on my compiler changing the order of the system.out.println() statement and even if I do
b.getH() + " " +b.h
I get
Beta 44 44 4
so here, the number comes last, as expected. Not sure why in the reverse the number does not come first.
Any help would be appreciated. Thank you.

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

Re: About Question enthuware.ocpjp.v17.2.3486 :

Post by admin »

The first print statement is: System.out.println(b.h + " " + b.getH());
In this statement, b.h + " " + b.getH() is an expression that has to be computed before println can print its value.
To compute the value of this expression, it joins b.h (which is 4, because the type of b is Baap and h is an instance field of Baap), " ", and the value returned by b.getH().

Now, to get the value returned by b.getH(), the method getH() has to be invoked. This method has a print statement System.out.println("Beta " + h), which prints "Beta 44". That is why this is the first line of the final output. This method then returns 44 after printing "Beta 44".

After getH() returns, the evaluation of the original expression will be completed as 4 44, which is what the print statement prints. Therefore the second line of the output is 4 44.

Post Reply

Who is online

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