Test
03-Objective-Wise-Encapsulation
Preguntas del test (5)
1Given:
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( ) ;
}v
oid updateArea( ) {
area = base*height/2;
}p
ublic void setBase( int b) { base = b; updateArea( ) ; }
public void setHeight( int h) { height = h; updateArea( ) ; }
}T
he 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?- Athe base field
- Bthe height field
- Cthe area field
- Dthe Triangle constructor
2What is meant by "encapsulation" ?
- AThere is no way to access member variable.
- BThere are no member variables.
- CMember fields are declared private but public accessor/mutator methods are
provided to access and change their values. - DData fields are declared public and accessor methods are provided to access and
change their values - ENone ofthe above.
3onsider 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; }
}p
ublic 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( ) ) ;
}
}Y
ou are assigned the task ofrefactoring the Square class to make it better in terms of
encapsulation. What changes will you make to this class?- Aprivate void calculateArea( ) {
this. area = this. side*this. side;
} - BMake side and area fields private.
- CChange setSide method to:
public void setSide( double d) {
this. side = d;
calculateArea( ) ;
} - DMake the getArea method public.
- EAdd a setArea( ) method:
public void setArea( double d) { area = d; }
- Aprivate void calculateArea( ) {
4When a class whose members should be accessible only to members ofthat class is
coded such a way that its members are accessible to other classes as well, this is
called ...- Astrong coupling
- Bweak coupling
- Cstrong typing
- Dweak encapsulation
- Eweak polymorphism
- Fhigh cohesion
5Consider 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;
}
}A
fter 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 ofthe following will
accomplish his goal?- AMake sum = 200;
- BMake sum = 200 and make it private.
- CMake sum = 200 and make all fields (radiusA, radiusB, and sum) private.
- DWrite another method setRadius2( int r) and set radiusB accordingly in this
method. - EHis goal cannot be accomplished.
- FThis class will not compile.
Información
- Descripción
- OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Test sobre 03-Objective-Wise-Encapsulation, creado por Wong Fei Hung el 01/08/2017.
- Autor
- Wong Fei Hung
- Idioma
- Português
- Visitas
- 1
- Etiquetas
- Valoración
- Aún sin valoraciones Valora este test