Encapsulation Question 1

  • Uploaded by: rahul rastogi
  • 0
  • 0
  • June 2020
  • 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 Question 1 as PDF for free.

More details

  • Words: 1,255
  • Pages: 5
Encapsulation Question 1 Which of the following are true statements? a. b. c. d. e.

Encapsulation is a form of data hiding. A tightly encapsulated class is always immutable. Encapsulation is always used to make programs run faster. Encapsulation helps to protect data from corruption. 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 data member that can be changed in any way; so a corruption. encapsulation helps to protect internal data from the 1 d Encapsulation allows for possibility of corruption from external influences. The e 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. b. c. d.

A top-level class can not be called "tightly encapsulated" unless it is declared private. Encapsulation enhances the maintainability of the code. A tightly encapsulated class allows fast public access to member fields. 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. b. c. d.

The class is declared final. All local variables are declared private. All method parameters are declared final. 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. c. d. e. f.

All of the methods are synchronized. All local variables are declared final. The class is a direct subclass of Object. Accessor methods are used to prevent fields from being set with invalid data. 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. b. c. d.

The data members can not be directly manipulated by external code. The class is declared final. It has no public mutator methods. The superclass is tightly encapsulated. ANSWER The data members can not be directly a 5 manipulated by external code. The d 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. b. c. d. e.

The class is a nested class. The constructors are declared private. The mutator methods are declared private. The class implements the Encapsulated interface. None of the above ANSWER None of the 6 e 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 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. b. c. d.

GFC501 GFC502 GFC503 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. b. c. d.

GFC503 GFC504 GFC505 None of the above ANSWER None of the 9 d 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? a. b. c. d.

GFC506 GFC507 GFC508 None of the above ANSWER 10 b GFC507 Class GFC507 has a public field; so it is not tightly encapsulated.

Related Documents

Protocol Encapsulation
April 2020 3
Encapsulation - Questions
October 2019 8
Encapsulation - Answers
October 2019 7
Question 1
November 2019 23
Question 1
April 2020 13

More Documents from "peppertune"