01.Given:
public class Triangle{
public int base;public int height;public double area;public Triangle(int base, int height){this.base = base; this.height = height;updateArea();}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 fieldB. the height fieldC. the area fieldD. the Triangle constructorE. the setBase methodF. the setHeight method
02.What is meant by
"encapsulation" ?
Select 1 option
A. There is no way to access member variable.B. There are no member variables.C. Member fields are declared private but public accessor/mutator methods areprovided to access and change their values.D. Data fields are declared public and accessor methods are provided to access andchange their values.E. None of the above.
03.Consider the following
two classes (in the same package but defined in different source files):
public class Square {
public class TestClass {double side = 0;double area;public Square(double length){ this.side = length; }public double getSide() { return side; }public void setSide(double side) { this.side = side; }double getArea() { return area; }}
public static void main(String[] args) throws Exception {
Square sq = new Square(10.0);sq.area = sq.getSide()*sq.getSide();System.out.println(sq.getArea());}
}
You are assigned the
task of refactoring the Square class to make it better in terms of encapsulation. What
changes will you make to this class?
Select 4 options
A. Add a calculateArea method:private void calculateArea(){this.area = this.side*this.side;}B. Make side and area fields private.C. Change setSide method to:public void setSide(double d){this.side = d;calculateArea();}D. Make the getArea method public.E. Add a setArea() method:public void setArea(double d){ area = d; }
04.When a class whose
members should be accessible only to members of that class is coded such a way that
its members are accessible to other classes as well, this is called ...
Select 1 option
A. strong couplingB. weak couplingC. strong typingD. weak encapsulationE. weak polymorphismF. high cohesionG. low cohesion
05.Consider the following
class written by a novice programmer.
class Elliptical{
public int radiusA, radiusB;public int sum = 100;public void setRadius(int r){if(r>99) throw new IllegalArgumentException();radiusA = r;radiusB = sum - radiusA;}
}
After some time, the
requirements changed and the programmer now wants to make sure that radiusB is
always (200 - radiusA) instead of (100 - radiusA) without breaking existing code
that other people have written. Which of the following will accomplish his goal?
Select 1 option
A. Make sum = 200;B. Make sum = 200 and make it private.C. Make sum = 200 and make all fields (radiusA, radiusB, and sum) private.D. Write another method setRadius2(int r) and set radiusB accordingly in thismethod.E. His goal cannot be accomplished.F. This class will not compile.
Very informative. Keep posting. azure training
RépondreSupprimerazure online training