GoConqr hat ein neues Design! Sag uns, wie es dir gefällt.

Quiz

03-Objective-Wise-Encapsulation

Wong Fei Hung 5 Fragen

Quiz starten

Fragen in diesem Quiz (5)

  1. 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?
    1. Athe base field
    2. Bthe height field
    3. Cthe area field
    4. Dthe Triangle constructor
  2. 2What is meant by "encapsulation" ?
    1. AThere is no way to access member variable.
    2. BThere are no member variables.
    3. CMember fields are declared private but public accessor/mutator methods are
      provided to access and change their values.
    4. DData fields are declared public and accessor methods are provided to access and
      change their values
    5. ENone ofthe above.
  3. 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?
    1. Aprivate void calculateArea( ) {
      this. area = this. side*this. side;
      }
    2. BMake side and area fields private.
    3. CChange setSide method to:
      public void setSide( double d) {
      this. side = d;
      calculateArea( ) ;
      }
    4. DMake the getArea method public.
    5. EAdd a setArea( ) method:
      public void setArea( double d) { area = d; }
  4. 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 ...
    1. Astrong coupling
    2. Bweak coupling
    3. Cstrong typing
    4. Dweak encapsulation
    5. Eweak polymorphism
    6. Fhigh cohesion
  5. 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?
    1. AMake sum = 200;
    2. BMake sum = 200 and make it private.
    3. CMake sum = 200 and make all fields (radiusA, radiusB, and sum) private.
    4. DWrite another method setRadius2( int r) and set radiusB accordingly in this
      method.
    5. EHis goal cannot be accomplished.
    6. FThis class will not compile.

Informationen

Beschreibung
OCA-SE-7 Objective-Wise-Ethuware-OCA-SE-7 Quiz am 03-Objective-Wise-Encapsulation, erstellt von Wong Fei Hung am 01/08/2017.
Sprache
Português
Aufrufe
1
Bewertung
Noch keine Bewertungen
Bewerte dieses Quiz
Bewerte dieses Quiz

Feedback

Worum geht es?

Wie gefällt dir das neue Design?

Dein Eindruck