Sie sind auf Seite 1von 5

Encapsulation

Question 1
Which of the following are true statements?

a. Encapsulation is a form of data hiding.


b. A tightly encapsulated class is always immutable.
c. Encapsulation is always used to make programs run faster.
d. Encapsulation helps to protect data from corruption.
e. Encapsulation allows for changes to the internal design of a class while the public
interface remains unchanged.

ANSWER
A tightly encapsulated class does not allow direct
public access to the internal data model. Instead,
access is permitted only through accessor (i.e. get) and
mutator (i.e. set) methods. The additional time
required to work through the accessor and mutator
Encapsulation is a form
methods typically slows execution speed.
of data hiding.
Encapsulation is a form of data hiding. A tightly
Encapsulation helps to
encapsulated class does not allow public access to any
protect data from
a data member that can be changed in any way; so
corruption.
1 d encapsulation helps to protect internal data from the
Encapsulation allows for
e possibility of corruption from external influences. The
changes to the internal
mutator methods can impose contraints on the
design of a class while
argument values. If an argument falls outside of the
the public interface
acceptable range, then a mutator method could throw
remains unchanged.
an IllegalArgumentException. The internal
design of a tightly encapsulated class can change while
the public interface remains unchanged. An immutable
class is always tightly encapsulated, but not every
tightly encapsulation class is immutable.

Question 2
Which of the following are true statements?

a. A top-level class can not be called "tightly encapsulated" unless it is declared private.
b. Encapsulation enhances the maintainability of the code.
c. A tightly encapsulated class allows fast public access to member fields.
d. A tightly encapsulated class allows access to data only through accessor and mutator
methods.
e. Encapsulation usually reduces the size of the code.
f. A tightly encapsulated class might have mutator methods that validate data before it is
loaded into the internal data model.

ANSWER
The data members of a tightly
encapsulated class are declared private;
Encapsulation enhances the so changes to the data model are less
maintainability of the code. A tightly likely to impact external code. Access to
encapsulated class allows access to internal data can be provided by public
b
data only through accessor and accessor (i.e. get) and mutator (i.e. set)
2 d
mutator methods. A tightly methods. The mutator methods can be
f
encapsulated class might have mutator used to validate the data before it is
methods that validate data before it is loaded into the internal data model. The
loaded into the internal data model. use of accessor and mutator methods is
likely to increase the size of the code and
slow execution speed.

Question 3
A class can not be called "tightly encapsulated" unless which of the following is true?

a. The class is declared final.


b. All local variables are declared private.
c. All method parameters are declared final.
d. No method returns a reference to any object that is referenced by an internal data
member.
e. None of the above

ANSWER
If a class A has a method that returns a reference to an internal, mutable
object; then external code can use the reference to modify the internal
None of
state of class A. Therefore, class A can not be considered tightly
3 e the
encapsulated. However, the methods of a tightly encapsulated class
above
may return a reference to an immutable object or a reference to a copy
or clone of an internal object.

Question 4
A class can not be called "tightly encapsulated" unless which of the following is true?

a. All of the methods are declared private.


b. All of the methods are synchronized.
c. All local variables are declared final.
d. The class is a direct subclass of Object.
e. Accessor methods are used to prevent fields from being set with invalid data.
f. None of the above

ANSWER
One answer option reads as follows: "Accessor methods are used to
prevent fields from being set with invalid data." The answer would be
None of correct if the word "Accessor" were replaced by the word "Mutator".
4 f the Accessor methods are used to read data members; mutator methods are
above used to set data members. The mutator methods can validate the
parameter values before the values are used to change the state of the
internal data model.

Question 5
A class can not be called "tightly encapsulated" unless which of the following are true?

a. The data members can not be directly manipulated by external code.


b. The class is declared final.
c. It has no public mutator methods.
d. The superclass is tightly encapsulated.

ANSWER
The data members can not be directly If a class A is not tightly
a
5 manipulated by external code. The encapsulated, then no subclass of A
d
superclass is tightly encapsulated. is tightly encapsulated.

Question 6
A class can not be called "tightly encapsulated" unless which of the following is true?

a. The class is a nested class.


b. The constructors are declared private.
c. The mutator methods are declared private.
d. The class implements the Encapsulated interface.
e. None of the above

ANSWER
None of the A tightly encapsulated class may have public mutator
6 e
above methods.
Question 7
A class can not be called "tightly encapsulated" unless which of the following is true?

a. All member fields are declared final.


b. The class is not anonymous.
c. The internal data model can be read and modified only through accessor and mutator
methods.
d. The class is an inner class.
e. None of the above

ANSWER
The internal data model can be A class is not tightly encapsulated if the internal
read and modified only data model can be read and/or modified without
7 c
through accessor and mutator working through accessor (i.e. get) and mutator
methods. (i.e. set) methods.

Question 8
class GFC500 {private String name;}
class GFC501 {
private String name;
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC502 {
private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}

Which class is not tightly encapsulated?

a. GFC501
b. GFC502
c. GFC503
d. None of the above

ANSWER
All three classes are tightly encapsulated, because the data members
None of
8 d are private. A tightly encapsulated class can have public accessor and
the above
mutator methods, but it is not required to have those methods.

Question 9
class GFC505 extends GFC504 {
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
class GFC504 extends GFC503 {
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC503 {String name;}

Which class is tightly encapsulated?

a. GFC503
b. GFC504
c. GFC505
d. None of the above

ANSWER
None of the Class GFC503 is not tightly encapsulated; so no subclass of
9 d
above GFC503 is tightly encapsulated.

Question 10
class GFC506 {private String name;}
class GFC507 extends GFC506 {
String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}
class GFC508 extends GFC506 {
private String name;
public GFC508(String name) {setName(name);}
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}

Which class is not tightly encapsulated?

a. GFC506
b. GFC507
c. GFC508
d. None of the above

ANSWER
10 b GFC507 Class GFC507 has a public field; so it is not tightly encapsulated.

Das könnte Ihnen auch gefallen