Java OCA OCP Practice Question 1390

Question

Given:

public class Main {
   public int base;
   public int height;
   public double area;

   public Main(int base, int height) {
      this.base = base;
      this.height = height;
      updateArea();/*w w  w  . jav a 2s . co  m*/
   }

   void updateArea() {
      area = base * height / 2;
   }

   public void setBase(int b) {
      base = b;
      updateArea();
   }

   public void setHeight(int h) {
      height = h;
      updateArea();
   }
}

The above class needs to protect an invariant on the "area" field.

Which three members must have the public access modifiers removed to ensure that the invariant is maintained?

Select 3 options

A. the base field 
B. the height field 
C. the area field 
D. the Main constructor 
E. the setBase method 
F. the setHeight method 


Correct Options are  : A B C

Note

An invariant means a certain condition that constrains the state stored in the object.

In this case the value of the area field of the Main must always be consistent with its base and height fields.

Thus, it should never have a value that is different from base*height/2.




PreviousNext

Related