Preview

06 - Encapsulation

 1. Encapsulation is seen as the bundling of data with the methods that operate on that data. Information hiding on the other hand is the principle that ______________________
Data Abstraction, Data Encapsulation and Information Hiding are often synonymously used in books and tutorials on OOP. But there is a difference

  all internal information can be edited, as would be expected

  some internal information or data is "hidden", so that it can't be accidentally changed

  all internal information is exposed when called

  no internal information or data is hidden

 2. Data encapsulation via methods doesn't necessarily mean that the data is hidden. You might be capable of accessing and seeing the data anyway, but using the methods is recommended

  FALSE

  TRUE

 3. Encapsulation is often accomplished by providing two kinds of methods for attributes: The methods for retrieving or accessing the values of attributes are called _______

  encapsulated methods

  getter methods

  setter methods

  None of the above

 4. Getter methods do not change the values of attributes, they just ___________________

  return the values

  create the objects

  delete the values

  change the values of the class attributes instead

 5. The methods used for changing the values of attributes are called ___________

  getter methods

  encapsulated methods

  setter methods

  None of the above

 6. Analyse the following code that uses getters and setters and predict the output
class Robot:
 
    def __init__(self, name=None):
        self.name = name   
        
    def say_hi(self):
        if self.name:
            print("Hi, I am " + self.name)
        else:
            print("Hi, I am a robot without a name")
            
    def set_name(self, name):
        self.name = name
        
    def get_name(self):
        return self.name
    

x = Robot()
x.set_name("Henry")
x.say_hi()
y = Robot()
y.set_name(x.get_name())
print(y.get_name())

  Henry

  Hi, I am self

  Hi, I am a robot without a name

  Hi, I am Henry Henry

 7. In Python, encapsulation is not enforced by the language, but the convention we use to indicate that a property is intended to be private is:

  to begin its name with a star(or double arrow)

  to begin its name with an 'x'

  None of the above

  to begin its name with an underscore

 8. It is also customary to set and get simple attribute values directly, and only write setter and getter methods for values which require some kind of calculation

  FALSE

  TRUE

 9. What is the output of the following code?
class Demo:
    def __init__(self):
        self.a = 1
        self.__b = 1
 
    def display(self):
        return self.__b
obj = Demo()
print(obj.a)

  The program has an error because b is private

  The program runs fine and 1 is printed

  The program has an error as you can’t name a class member using __b

  a, b

 10. The purpose of getters and setters is to get(return) and set(assign) private instance variables of a class.

  FALSE

  TRUE

 11. These attributes can be freely used inside or outside of a class definition.

  protected

  public

  private

  self

 12. These attributes should not be used outside of the class definition, unless inside of a subclass definition.

  private

  protected

  public

  self

 13. This kind of attribute is inaccessible and invisible. It's neither possible to read nor write to those attributes, except inside of the class definition itself.

  public

  private

  protected

  self

 14. Encapsulation can also be defined as the process of combining data and functions into a single unit called class

  FALSE

  TRUE

 15. In this Java scode a hacker tries to deposit an invalid amount (-100) to your account. What happens?
class Account {
    private int account_number;
    private int account_balance;

    public void show Data() {
        //code to show data 
    }

    public void deposit(int a) {
        if (a < 0) {
            //show error 
        } else
            account_balance = account_balance + a;
    }
}

#Hacker tries the following:
class Hacker{
Account a = newAccount();
a.account_balance= - 100;
    {

  He will not be able to do so as the account balance is private

  He will be able to do so - the program is not secure

  None of the above

  He will be able to not only do so, but also clear your balance