About Question enthuware.ocpjp.v7.2.1111 :

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

Moderator: admin

Post Reply
Ryhor

About Question enthuware.ocpjp.v7.2.1111 :

Post by Ryhor »

Who can explain why the code doesn't need to have a constructor without parameters in class B to be compiled without errors?

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

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

Post by admin »

Class B does need a no-args constructor because the constructors of class C depend on that. But since class B does not define any constructor explicitly, the compiler provides the no args constructor to class B automatically.

As the explanation says, "Because //B1 is not a constructor. Note that it is returning an int."

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

Ryhor

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

Post by Ryhor »

Yeah! How I couldn't notice that! Class B does not provide any constructor explicitly...
Thanks!

petlju
Posts: 1
Joined: Sat Oct 26, 2013 4:14 am
Contact:

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

Post by petlju »

"One of the constructors of each class is called as a result of constructing an object of class C.
To create any object one and only one constructor of that class and each of the super classes is called."

But how that does work with constructor overloading and this();?

Is it then not correct to say that two constructors were called for that class? One explicitly and one implicitly?

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

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

Post by admin »

Calling this(...) is not much different from calling any method in a constructor. The explanation is talking about constructors that are called by the JVM as a result of doing new on a Class. The JVM calls exactly one constructor.

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

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post by sarakh »

The code says

Code: Select all

boolean b1 = false; 
boolean b2  = false; 
if (b2 = b1 == false){    System.out.println("true"); } 
else{    System.out.println("false"); } 
And the explanation says
All that if() needs is a boolean, now b1 == false returns true, which is a boolean and since b2 = true is an expression and every expression has a return value (which is the Left Hand Side of the expression), it returns true, which is again a boolean. FYI: the return value of expression  i = 10;  is 10 (an int).
So the order of execution is:
1. b1 == false
2. b2 = result of 1

we also have
enthuware.ocajp.i.v7.2.949
with code

Code: Select all

boolean b1 = false;
boolean b2 = false;
if (b2 != b1 = !b2){
   System.out.println("true");
}
else{
   System.out.println("false");
} 
and explanation
Note that boolean operators have more precedence than =. (In fact, = has least precedence of all operators.)
so, in (b2 != b1 = !b2) first b2 != b1 is evaluated which returns a value 'false'. So the expression becomes false = !b2. And this is illegal because false is a value and not a variable!
So the order of execution is:
1. b2 != b1
2. result of 1 = !b2

So if I want to talk about the order of evaluation of =, != and ==, can I say:
- first != or == is evaluated, whichever comes first in the code
- then = is evaluated.

Is that correct?

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

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

Post by admin »

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

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

I was surprised to see the Code compilation fails to be incorrect only to find later that
the line
public int B(String s) { System.out.println("B :"+s); return 0; } // B1
is valid!! I think you should also add the explanation for this line too! Since it isn't read the following link:
http://stackoverflow.com/questions/3401 ... ructor-why

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

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

Post by admin »

It does include an explanation under this option that tells that it is valid method and not a constructor.
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

admin wrote:It does include an explanation under this option that tells that it is valid method and not a constructor.
Yes it does. But it doesn't say that such methods are allowed.
Also the below code also compiles which is suprising to me:
public B()
{

}
public int B() { System.out.println("B :"); return 0; } // B1
}
Great question btw!

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

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

Post by admin »

If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
If you like our products and services, please help us by posting your review here.

jagoneye
Posts: 97
Joined: Wed Dec 28, 2016 9:00 am
Contact:

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

Post by jagoneye »

admin wrote:If something is valid doesn't that mean it is allowed? It is allowed that is why it is valid, no?
True I was lost somewhere during this test I guess to not follow the implications.
:)

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests