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

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

Moderator: admin

Post Reply
Deleted User 2655

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

Post by Deleted User 2655 »

Can someone please explain to me why;

Code: Select all

bw.addMore(-bw.getAvailable()); will print 0?

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

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

Post by admin »

bw.getAvailable() returns 100, therefore when you pass -bw.getAvailable() to addMore, you are essentially passing -100 . addMore() performs 100+ (-100), which makes available = 0. That is why System.out.println(bw.getAvailable()); prints 0.

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

Deleted User 2655

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

Post by Deleted User 2655 »

Thanks Paul.

Rinkesh
Posts: 35
Joined: Sat Nov 25, 2017 4:13 pm
Contact:

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

Post by Rinkesh »

Why is bw(0) wrong??

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

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

Post by admin »

Rinkesh wrote:Why is bw(0) wrong??
Well, this is a very basic programming question. bw is a variable and () can only be applied to a method name while invoking that method on a variable. For example, bw.addMore(0). So not sure why you think bw(0) makes sense? What do you think bw(0) means?

Looking at other questions that you have asked, I will suggest you to go through a good Java book to understand the basics before attempting mock exams. Otherwise, mock exams will not help you as much.

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

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

Post by wdphipps »

Hi team,

Hoping you can clear up some confusion for me.

I see in this question the explanation "Bandwidth class does not have a no-args constructor so this will not compile." is given as the reason for "bw = new Bandwidth();" being incorrect.

Wouldn't Java create a default constructor in this example?

My original guess is that this line is important:

"and a piece of code from another class:"

Thanks in advance,

-WP

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

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

Post by admin »

The default no args constructor is provided by the compiler only when there is no other constructor present in the class.

Which book are you following?
If you like our products and services, please help us by posting your review here.

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

Post by wdphipps »

I'm using the latest book.

I looked further into the default constructor, it seems like I gained some understanding getting this wrong. I had understood that without a *no-args* constructor, a default would be created by the compiler. I know now that a default is only created if *no* constructor is given at all. This was also in the book:

"The compiler will complain that Account class does not have a constructor that takes no arguments. What happened to the default constructor, you ask? Well, since this class provides a constructor explicitly, the compiler feels no need to add one on its own."

Thanks for the help!

wdphipps
Posts: 21
Joined: Mon Sep 23, 2019 4:55 pm
Contact:

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

Post by wdphipps »

Now that i'm further in the book, I see how unnecessary my asking this question was. The default constructor is discussed at length further along this version of the book. Thanks again, team.

Post Reply

Who is online

Users browsing this forum: No registered users and 45 guests