About Question enthuware.ocajp.i.v7.2.1288 :

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

Moderator: admin

Post Reply
patrickK
Posts: 3
Joined: Sun Jun 03, 2012 5:45 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1288 :

Post by patrickK »

I understand the explanation of this question. However, I don't see why there are four lines of output, when println() is only called twice. How does getH() execute its println() when it's only used for its return value? Does using b.getH() and bb.getH() as parameters cause them to execute their println() calls? And if that is the case, wouldn't 4 get printed first, because it comes first in the first println() call in main()?

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

There is no parameter being passed to getH() calls. The code will not compile if you pass a parameter because the method declares no parameters.

How do you think a method will return a value? It returns a value after it executes. So in this case, both the getH methods (the base class one and the subclass one) have a system.out.println with in their body, which prints the additional two lines that you see.
For example, the line System.out.println(b.h+" "+b.getH()); actually prints two lines of output and not just one because one line of output is coming from the getH() method.

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

Guest

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by Guest »

I understand that instance variables and static methods are shadowed and that instance methods are overridden but I can not comprehend why the System.out.println() of Baap is printed before the first called b.h and again in bb.h.

In other words it seems like a number would be printed first, then the S.O.P. statement, then another number with the return.

Instead the S.O.P. prints then the individual numbers... 0_o

It almost seems like there is a static initialization block or S.O.P. statements in a constructor running but this is not the case.

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

Guest wrote:I understand that instance variables and static methods are shadowed and that instance methods are overridden but I can not comprehend why the System.out.println() of Baap is printed before the first called b.h and again in bb.h.

In other words it seems like a number would be printed first, then the S.O.P. statement, then another number with the return.

Instead the S.O.P. prints then the individual numbers... 0_o

It almost seems like there is a static initialization block or S.O.P. statements in a constructor running but this is not the case.
This is what happens when the first SOP in main ( System.out.println(b.h+" "+b.getH()); )executes:
1. For the call to go inside the println method, you first need to compute the parameter that is to be passed. So, b.h+" "+b.getH() has to be computed first.
2. Now, to compute this string, you start putting the values of the 3 components -
4 + " "+b.getH() <-- So you see that b.4 is indeed accessed first as you say.
3. Now, you need to execute b.getH() to complete the third component. It returns 44 but this method contains a println of its own, so before returning 44, it prints "beta 44".
4. So now, your string becomes - 4+ " "+44, which is then printed out.

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

Javanaut

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by Javanaut »

Hi Paul,

Thank-you sir for the reply. I wrote a similar class and obtained the same behavior. Thank-you for the explanation, that made sense. I was not expecting the method's SOP to print before the SOP in main().

:shock: :mrgreen:


Respectfully,

Javanaut

Guest

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by Guest »

Could you please explain why following code not function in same manner?

Code: Select all

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
} 
Here output is 8 Accelerate : SportsCar

It prints the gearRatio value from the parent class

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

Guest wrote:Could you please explain why following code not function in same manner?

Code: Select all

class Car{
   public int gearRatio = 8;
   public String accelerate() {  return "Accelerate : Car";  }
}
class SportsCar extends Car{
   public int gearRatio = 9;
   public String accelerate() {  return  "Accelerate : SportsCar";  }
   public static void main(String[] args){
      Car c = new SportsCar();
      System.out.println( c.gearRatio+"  "+c.accelerate() );
   }
} 
Here output is 8 Accelerate : SportsCar

It prints the gearRatio value from the parent class
Because unlike methods, variables are shadowed instead of overridden. So here, you are printing c.gearRatio, where c is of type Car. That is why Car's gear ratio is printed. By using a variable of super class, you are un-shadowing the Car's shadowed gearratio field.

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

satar
Posts: 10
Joined: Tue Feb 12, 2013 9:12 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by satar »

Interesting, this could really be bad because if you have an overriding method that accesses an overshadowed variable, you can create a conundrum where you can update, in our example b.h but it never actually gets updated if you access it only through b.getH(). A very good example of one reason why you always want to encapsulate (make private) class variables. I would have never thought this kind of thing was possible. These exams are very clever about identifying idiosyncrasies with the language providing a major education in themselves!

AlsoConfused.java

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by AlsoConfused.java »

So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

AlsoConfused.java wrote:So the printout should be
for b.h (4 Beta 44 44) &
for bb.h (44 Beta 44 44)?
Yes.
If you like our products and services, please help us by posting your review here.

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by The_Nick »

Hi,
I would like to know why it prints out Beta 44 4 first and not 4 Beta 44 as I expected to be. Is it not executed from left to right? Thanks in advance


The_Nick

The_Nick

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by The_Nick »

It must mean that the method getH() gets executed straight away without waiting the end of the statement evaluation, do you confirm this thesis?
Thanks in advance.

The_Nick

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

The_Nick wrote:It must mean that the method getH() gets executed straight away without waiting the end of the statement evaluation, do you confirm this thesis?
Thanks in advance.

The_Nick
Yes.
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.ocajp.i.v7.2.1288 :

Post by jbilkes »

This concept of first 'calculating' String-values by executing all methods inside is totally new to me. It should definitely be mentioned in the answer because its a) important and b) never mentioned before as far as I know, thx

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

Not sure what you mean. There is nothing new in the question with respect to printing Strings using System.out.println.
Concatenating Strings is covered in detail in String related questions.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by gparLondon »

I executed this code. The o/p is

Beta 44 4 44
Beta 44 44 44.

Which was the same as answer given by the author. But, my question is, why the answer is not

4 Beta 44 44
44 Beta 44 44?
:roll:
Why is it printing a String first than int? as in println of main first statement is b.h?
I may be asking the same question asked by many, but I am asking this, because none of the answers given above are clear to me.

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

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by admin »

No, the first print statement is not just b.h. It is: b.h+" "+b.getH()
Before the println method can be executed, its argument has to be evaluated. So it has to first compute the value of b.h+" "+b.getH(). To compute its value, it needs to call b.getH(). And this call causes other println() to execute.

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

disznoperzselo
Posts: 28
Joined: Fri Jan 02, 2015 12:13 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1288 :

Post by disznoperzselo »

Next time somebody says that "OCAJP is easy" I will simply train my gun on them like Samuel L. Jackson in Pulp Fiction and say that "Say OCAJP is EASY one more time"! ... And then I will ask them this easy question and see how they perform under pressure :) I hope they will understand at last why passing OCAJP is hard indeed ..

Post Reply

Who is online

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