Encapsulation - Answers

  • October 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Encapsulation - Answers as PDF for free.

More details

  • Words: 641
  • Pages: 1
Encapsulation

Answers

Answers: Certified Java Programmer Mock Exam No.

Answer

Remark

1

A tightly encapsulated class is always b immutable. Encapsulation typically allows c programs to run faster.

This question asks for the statements that are NOT true. 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. The mutable data members of a tightly encapsulated class are declared private; so encapsulation helps to protect internal data from the possibility of corruption from external influences. Public access to internal data can be provided by public accessor and mutator methods. The mutator methods can validate the data before it is loaded into the internal data model. 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.

2

Encapsulation enhances the maintainability of the code. A tightly encapsulated class allows b access to data only through accessor and d mutator methods. A tightly encapsulated class f 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.

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.

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.

5

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

If a class A is not tightly encapsulated, then no subclass of A is tightly encapsulated.

6

e None of the above

A tightly encapsulated class may have public mutator methods. If a tightly encapsulated class does not have non-private mutator methods, then the class can be called immutable.

7

The internal data model can be read and c 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.

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.

9

d None of the above

Class GFC503 is not tightly encapsulated; so no subclass of GFC503 is tightly encapsulated.

3

10 b GFC507

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

page:1

Related Documents

Encapsulation - Answers
October 2019 7
Protocol Encapsulation
April 2020 3
Encapsulation - Questions
October 2019 8
Wrapping The Encapsulation)
November 2019 13
Answers
April 2020 22
Answers
April 2020 24