8
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 1a d e Encapsulation is a form of data hiding. Encapsulation helps to protect data from corruption. Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged. 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 methods typically slows execution speed. Encapsulation is a form of data hiding. A tightly encapsulated class does not allow public access to any data member that can be changed in any way; so encapsulation helps to protect internal data from the possibility of corruption from external influences. The mutator methods can impose contraints on the

Encapsulation Question 1

Embed Size (px)

DESCRIPTION

rahul rastogi

Citation preview

Page 1: Encapsulation Question 1

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

1a  d  e 

Encapsulation is a form of data hiding.  Encapsulation helps to protect data from corruption.  Encapsulation allows for changes to the internal design of a class while the public interface remains unchanged. 

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 methods typically slows execution speed. Encapsulation is a form of data hiding. A tightly encapsulated class does not allow public access to any data member that can be changed in any way; so encapsulation helps to protect internal data from the possibility of corruption from external influences. The mutator methods can impose contraints on the argument values. If an argument falls outside of the acceptable range, then a mutator method could throw 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.

Page 2: Encapsulation Question 1

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

2b  d  f 

Encapsulation enhances the maintainability of the code.  A tightly encapsulated class allows access to data only through accessor and mutator methods.  A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model. 

The data members of a tightly encapsulated class are declared private; so changes to the data model are less likely to impact external code. Access to internal data can be provided by public accessor (i.e. get) and mutator (i.e. set) methods. The mutator methods can be used to validate the data before it is loaded into the internal data model. The 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

3 e  None of the above 

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 state of class A. Therefore, class A can not be considered tightly encapsulated. However, the methods of a tightly encapsulated class may return a reference to an immutable object or a reference to a copy or clone of an internal object.  

Question 4

Page 3: Encapsulation Question 1

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

4 f  None of the above 

One answer option reads as follows: "Accessor methods are used to prevent fields from being set with invalid data." The answer would be correct if the word "Accessor" were replaced by the word "Mutator". Accessor methods are used to read data members; mutator methods are 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

5a  d 

The data members can not be directly manipulated by external code.  The superclass is tightly encapsulated. 

If a class A is not tightly encapsulated, then no subclass of A 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.

Page 4: Encapsulation Question 1

e.  None of the above

ANSWER

6 e  None of the above  A tightly encapsulated class may have public mutator 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

7 c 

The internal data model can be read and modified only through accessor and mutator methods. 

A class is not tightly encapsulated if the internal data model can be read and/or modified without working through accessor (i.e. get) and mutator (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.  GFC501b.  GFC502c.  GFC503d.  None of the above

Page 5: Encapsulation Question 1

ANSWER

8 d  None of the above 

All three classes are tightly encapsulated, because the data members are private. A tightly encapsulated class can have public accessor and 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.  GFC503b.  GFC504c.  GFC505d.  None of the above

ANSWER

9 d  None of the above 

Class GFC503 is not tightly encapsulated; so no subclass of 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?

Page 6: Encapsulation Question 1

a.  GFC506b.  GFC507c.  GFC508d.  None of the above

ANSWER

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