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

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

Moderator: admin

Post Reply
fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

calling System.out.print cause the toString method called or making a new object from the class SubClass? or both?

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

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

Post by admin »

The line of code is System.out.println( new SubClass() ); so first a new SubClass object will be created and that object will be passed to the println method. The method will then cause the toString method on that object to be invoked.
If you like our products and services, please help us by posting your review here.

AristPhil
Posts: 4
Joined: Wed Jan 06, 2016 5:38 am
Contact:

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

Post by AristPhil »

Sirs, the exact answer to this questions should be 43 (i.e. option 1). The (console) output is 43 - not "43".
So, why is option 1 (i.e. 43) wrong?

I. Hypothesis:
I guess that you do not differentiate between the asked output and your supposed return value of System.out.println( new SubClass() ) in SubClass's main method. Although the return value is a String, the program prints 43 - not "43".

II. Explanation:
You explain your desired answer (i.e. option 5 - None of the above.) like this:
So the final value is "43".
Nevertheless, the question asks for the print out - not the final value:
What will the following program print when run?
III. Suggestion:
If my reasoning is correct, wouldn't it be helpful either to ask for the return value (w/o return type) or to change the correct answer to option 1?

AristPhil

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

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

Post by admin »

The question asks, "What will the following program print when run?" and the correct option i.e. option 1, says 43. The option does not say "43".

Yes, the explanation says "43" because it is explaining about the final value of the statement super.toString()+"3";
Not sure what you see wrong with that.

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

AristPhil
Posts: 4
Joined: Wed Jan 06, 2016 5:38 am
Contact:

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

Post by AristPhil »

Sorry, my fault: I misinterpreted the tool's evaluation view! Initially, I answered the question wrong. During my review the tool highlighted the correct option in green, i.e. option 1 saying 43, but I thought this was my (wrong) answer. It was the correct answer instead which I had selected during my review and which I should have selected during the exam as well. Sorry for the confusion, Paul! My text above says nothing else than your correct answer and explanation.

CagriKahraman
Posts: 2
Joined: Sun Oct 15, 2017 9:57 pm
Contact:

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

Post by CagriKahraman »

Hi Admin,

I just passed the OCA exam, and I got at least 3 questions about toString overriding and I have a suggestion about this questions. I wouldn't understand the reason how come when you print the object as (new Something()), it prints out the objects with toString method until I solve this toString question on Last day test. It's explanation help a lot to understands how toString methods works, So you would add this explanation to this question.
Here is your explanation of enthuware.ocajp.i.v8.2.1308 :

"The toString method for class Object returns a String consisting of the name of the class of which the object is an instance, the at-sign character '@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of: getClass().getName() + '@' + Integer.toHexString(hashCode())"

My suggestion is adding some more detail about how the implementation of toString method in the class and when you print out the object, how toString method effects it.

Thanks a lot, we are lucky to have Enthuware!!!

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

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

Post by admin »

Thank you for the feedback, Cagri. We will incorporate it into the questions asap.
Paul.
If you like our products and services, please help us by posting your review here.

sadhiunni
Posts: 1
Joined: Sat May 26, 2018 5:35 am
Contact:

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

Post by sadhiunni »

isn't Super a keyword?
so can we give Super as a class name?

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

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

Post by admin »

Super isn't a keyword. super is.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

I thought the toString method of the Super class is overridden and therefor 'replaced'. However, appearently it is kept alive as SubClass's toString method refers to it. Is that correct?
And, as Object is the super class of Super class and also had the toString method, is it possible to call Object's toString method and still print SubClass@hexcode from the main method (in other words, is it possible to invoke the Object's toString method or is that replaced because it's not referred to from the class Super)?

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

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

Post by admin »

>I thought the toString method of the Super class is overridden and therefor 'replaced'.

yes, 'replaced' in the subclass. So, SubClass has its own version of toString. Super class still has its own version, which can be invoked by SubClass's toString using super.toString() syntax.

You might have misunderstood the "replace" thing. If a subclass overrides a method then that methods gets replaced in the subclass. i.e. the subclass doesn't inherit super class's version of the method anymore (because it has its own version of that method now). The method doesn't get replaced in the super class. Java provides a special syntax (super dot) using which a subclass method can invoke the super class's version of the overridden method. But this syntax allows you to go only one level up. You can't do super.super.toString().

So, you cannot invoke Object's version of toString from SubClass's version of toString, in this case.
If you like our products and services, please help us by posting your review here.

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

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

Post by qazwsx922 »

I thought System.out.println( new SubClass() ); this line should be System.out.println(new SubClass().toString()); to print 43

how new SubClass() calls toString method?

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

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

Post by admin »

new SubClass() does NOT call toString method. The println method calls toString on the object that is passed to it.

Please see this for exact details: https://docs.oracle.com/en/java/javase/ ... ng.Object)
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 31 guests