Exercise: Encapsulation
Questions for: Encapsulation
What is the role of a setter method in encapsulation?
A:
To retrieve the value of a private variable.
B:
To provide a static method for a class.
C:
To set the value of a private variable with validation.
D:
To define a private variable.
Answer: C
A setter method in encapsulation is used to set the value of a private variable with validation, ensuring controlled access.
Which of the following is true about encapsulation?
A:
It involves exposing all implementation details of an object.
B:
It is achieved through the use of global variables.
C:
It allows for unrestricted access to internal details of an object.
D:
It combines data and methods into a single unit and hides implementation details.
Answer: D
Encapsulation in Python combines data and methods into a single unit and hides implementation details.
Discuss About this Question.
What is the primary benefit of using encapsulation in object-oriented programming?
A:
It allows unrestricted access to the internal details of an object.
B:
It promotes code reusability.
C:
It enhances data security by hiding implementation details.
D:
It simplifies the process of class instantiation.
Answer: C
The primary benefit of using encapsulation is that it enhances data security by hiding the implementation details of an object.
Discuss About this Question.
In Python, what does the double underscore prefix in a variable, like
__variable, indicate?
A:
A protected variable
B:
A private variable
C:
A global variable
D:
A constant variable
Answer: B
The double underscore prefix in Python, as in
__variable, indicates a private variable.Discuss About this Question.
Consider the following Python code:
class Car:
def __init__(self, model, year):
self.__model = model
self.__year = year
def display_info(self):
return f"Model: {self.__model}, Year: {self.__year}"
What concept of encapsulation is demonstrated in this code?
A:
Public access specifier
B:
Private access specifier
C:
Protected access specifier
D:
Global variable
Answer: B
The double underscore prefix (
__) in the variables __model and __year indicates that they are private variables, demonstrating encapsulation by hiding the implementation details.Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.