exam questions

Exam 1z0-808 All Questions

View all questions & answers for the 1z0-808 exam

Exam 1z0-808 topic 1 question 9 discussion

Actual exam question from Oracle's 1z0-808
Question #: 9
Topic #: 1
[All 1z0-808 Questions]

Given these two classes:

Any amount of electricity used by a customer (represented by an instance of the Customer class) must contribute to the customer's bill (represented by the member variable bill) through the useElectricity method.
An instance of the Customer class should never be able to tamper with or decrease the value of the member variable bill.
How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?
A.

B.

C.

D.

Show Suggested Answer Hide Answer
Suggested Answer: A

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
Vicky_65
Highly Voted 1 year, 8 months ago
Answer is B
upvoted 10 times
...
TanmoyB
Highly Voted 1 year, 11 months ago
Answer is B
upvoted 5 times
...
MPignaProTech
Most Recent 2 months ago
You must check positif value: B
upvoted 1 times
The_Java_Champion
2 days, 8 hours ago
You would be correct if the condition was if(kWh >= 0) but since they are checking if kWh is strictly greater than 0 kWh > 0 then its value will never be increased. Because when you first create an Instance of ElectricAccount, The kWh variable is not initialized and set to 0 by default . So when you call the addKWh method it will always find that kWh is 0 inside the if the statement and 0 is not strictly greater than 0 so the code inside the if statement is ignored and you are always stuck with the same 0 value.
upvoted 1 times
...
...
iheb07
7 months, 4 weeks ago
Answer is B
upvoted 1 times
...
Sezam
1 year, 3 months ago
Answer is B
upvoted 2 times
...
a_really_reliable_programmer
1 year, 3 months ago
Answer is B. A may output negative B. C will compile error (Cannot call private) D, the setBill can be called by customer (Check line 2 of the rule) Credits to Hirushi
upvoted 2 times
...
mbns
1 year, 4 months ago
The correct answer is C. All other methods are public. The method must be private that the customer can not change it
upvoted 1 times
tawa_z58
11 months, 2 weeks ago
then how can that method be called from the customer class since we need to call it. if we want the customer not to change, we have to make sure that all our implementations are not giving the customer access to changing the bill by either going up or down. making the kwh method private will cause an error as the method wont be visible in the Customer class.
upvoted 1 times
...
...
Hirushi
2 years ago
Code for future refer: class HelloWorld { public static void main(String[] args) { System.out.println("Hi"); Customer c = new Customer(); //c.useElectircity(100); //c.useElectircity(200); c.useElectircity(-100); } }
upvoted 3 times
Hirushi
2 years ago
/* //C --> COMPILE ERROR (addKWh(double) has private access in ElectricAccount) private void addKWh(double kWh){ if(kWh > 0){ this.kWh += kWh; this.bill = this.kWh*this.rate; } System.out.println(bill); } */
upvoted 1 times
...
Hirushi
2 years ago
class ElectricAccount{ private double kWh; private double rate = 0.07; private double bill; //line 1 (It will provide upcoming 4 replies) }
upvoted 1 times
...
Hirushi
2 years ago
class Customer{ ElectricAccount acct = new ElectricAccount(); public void useElectircity(double kWh){ acct.addKWh(kWh); } }
upvoted 1 times
Hirushi
2 years ago
class ElectricAccount{ private double kWh; private double rate = 0.07; private double bill; //line 1 (It will provide upcoming 4 replies) }
upvoted 1 times
Hirushi
2 years ago
/* //A --> print correct answer, minus answer prints with minus(-) values public void addKWh(double kWh){ System.out.println(bill); // (only one call)0.0 //(call twice)0.0 7.000000000000001 this.kWh += kWh; this.bill = this.kWh*this.rate; System.out.println(bill); //(only one call)7.000000000000001 //(call twice)7.000000000000001 21.000000000000004 //???minus bill amount calculate for minus(-) values } */
upvoted 1 times
...
...
...
Hirushi
2 years ago
/* //B --> print correct answer, when minus(-) value pass displays 0.0 public void addKWh(double kWh){ if(kWh > 0){ this.kWh += kWh; this.bill = this.kWh*this.rate; } System.out.println(bill); //1st call -> 7.000000000000001 //2nd call -> 21.000000000000004 //0.0 for minus(-) value } */
upvoted 1 times
...
...
Hirushi
2 years ago
Here, both A and B give the correct answer and the only difference is, when we put the minus(-) value to useElectircity() method, A will calculate the bill and provide the negative answer. B always calcluates bill as 0.0. I also think B is the answer the first time. because it validates the Kwh and also compiles successfully. Doesn't tamper with the bill variable as well. But I have another confusing point, In question last line saying like this, "member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?" Here, it says member variable always equal to the value of the member variable kwh multiplied by the member variable rate. So, I have a point answer is A. can anyone explain this? my point is valid or not???
upvoted 4 times
...
Hirushi
2 years ago
Anyone know the exact correct answer?
upvoted 1 times
...
Def8
2 years, 1 month ago
Correct answer is B as it validates the Kwh and also compiles successfully. Doesn't tamper with the bill variable as well.
upvoted 1 times
...
carloswork
2 years, 1 month ago
The answer is B Because: Option A: Does not validate the kwh arguments, and could be receive negative "watts". Option C: The addKwh method was declared private, it is not accessible in the Customer class. Option D: The bill member variable is not being changed, only the local scope of the setBill method is being used.
upvoted 2 times
...
iSnover
2 years, 2 months ago
The answer is B and no need to go far, all other options are syntax error. Only the letter B remains.
upvoted 1 times
...
Rdharma
2 years, 2 months ago
Answer is B
upvoted 1 times
...
shivkumarx
2 years, 3 months ago
Answer should be B
upvoted 4 times
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...
exam
Someone Bought Contributor Access for:
SY0-701
London, 1 minute ago