Exercise: Python

Questions for: Encapsulation

What is the purpose of the @property decorator?
A:
To define a class property
B:
To create a class instance
C:
To access a class attribute
D:
To define a getter method for a class attribute
Answer: D
The @property decorator is used in Python to define a getter method for a class attribute, allowing controlled access.
Which access specifier in Python is used to indicate that a variable or method should only be accessed within the same class?
A:
Public
B:
Private
C:
Protected
D:
Global
Answer: B
Private access specifier in Python is denoted by a single underscore (_), and it indicates that the variable or method should only be accessed within the same class.
In Python, what is encapsulation?
A:
The process of hiding the implementation details of an object and exposing only the necessary functionalities
B:
The process of combining data and methods into a single unit
C:
The process of creating multiple instances of a class
D:
The process of inheriting attributes and behaviors from another class
Answer: A
Encapsulation in Python involves bundling the data and methods of a class and controlling access to the internal details, exposing only the necessary functionalities.
Ad Slot (Above Pagination)
Quiz