nerdexam
Oracle

1Z0-808 · Question #44

1Z0-808 Question #44: Real Exam Question with Answer & Explanation

Sign in or unlock 1Z0-808 to reveal the answer and full explanation for question #44. The question stem and answer options stay visible for context.

Working with Methods and Encapsulation

Question

Given the following two classes:
public class Customer {
 ElectricAccount acct = new ElectricAccount();

 public void useElectricity(double kWh) {
 acct.addKwh(kWh);
 }
}

public class ElectricAccount {
 private double kwh;
 private double rate = 0.07;
 private double bill;

 //line n1
}
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? 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 method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.

Options

  • A
    public void addKwh(double kWh) {
     this.kwh += kWh;
     this.bill = this.kwh*this.rate;
    }
    
  • B
    public void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     this.bill = this.kwh * this.rate;
     }
    }
    
  • C
    private void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     this.bill = this.kwh*this.rate;
     }
    }
    
  • D
    public void addKwh(double kWh) {
     if (kWh > 0) {
     this.kwh += kWh;
     setBill(this.kwh);
     }
    }
    
    public void setBill(double kWh) {
     bill = kWh*rate;
    }
    

Unlock 1Z0-808 to see the answer

You've previewed enough free 1Z0-808 questions. Unlock 1Z0-808 for full answers, explanations, the timed quiz mode, progress tracking, and the master PDF. Question stem and options stay visible so you can still see what's on the exam.

Topics

#encapsulation#access modifiers#class invariants#data protection
Full 1Z0-808 Practice