Exercise: Encapsulation
Questions for: Encapsulation
What is the role of a getter method in encapsulation?
A:
To set the value of a private variable
B:
To retrieve the value of a private variable
C:
To create a new instance of a class
D:
To delete a private variable
Answer: B
A getter method in encapsulation is used to retrieve the value of a private variable.
Which statement accurately describes the concept of encapsulation?
A:
Encapsulation involves exposing all implementation details of an object.
B:
Encapsulation is a mechanism for achieving multiple inheritances.
C:
Encapsulation combines data and methods into a single unit and hides implementation details.
D:
Encapsulation is only applicable to global variables.
Answer: C
Encapsulation in Python combines data and methods into a single unit and hides implementation details.
Discuss About this Question.
In Python, what is the purpose of using a double underscore prefix in a method name, such as
__method()?
A:
To define a private method
B:
To create a public method
C:
To indicate a class method
D:
To access a global method
Answer: A
The double underscore prefix in a method name in Python, such as
__method(), is used to define a private method.Discuss About this Question.
Which keyword in Python is used to define a private variable within a class?
A:
private
B:
secret
C:
protected
D:
None of the above
Answer: D
In Python, there is no explicit keyword for defining private variables. Conventionally, a single or double underscore prefix is used to indicate privacy, but it is a convention and not a strict rule.
Discuss About this Question.
Consider the following Python code:
class Product:
def __init__(self, name, price):
self._name = name
self._price = price
@property
def get_price(self):
return self._price
What is the purpose of the @property decorator in this code?
A:
To define a private variable
B:
To create a class instance
C:
To provide a getter method for a private variable
D:
To access a global variable
Answer: C
The
@property decorator in this code is used to provide a getter method for the private variable _price.Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.