latest articles

Recent Posts Widget

Encapsulation QCM1


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 field
B. the height field
C. the area field
D. the Triangle constructor
E. the setBase method
F. 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 are
provided to access and change their values.
D. Data fields are declared public and accessor methods are provided to access and
change 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 {
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 class TestClass {
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 coupling
B. weak coupling
C. strong typing
D. weak encapsulation
E. weak polymorphism
F. high cohesion
G. 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 this
method.
E. His goal cannot be accomplished.
F. This class will not compile.

1 commentaire: