Python Programming - Classes

Exercise : Classes - General Questions
  • Classes - General Questions
11.
What is the purpose of the @property decorator?
It defines a property of a class that can be accessed using dot notation.
It marks a method as a getter for a class attribute.
It is used to create an instance variable in a class.
It indicates a method that can only be accessed by the class itself.
Answer: Option
Explanation:
The @property decorator is used to define a getter method for a class attribute. It allows the method to be accessed like an attribute without using parentheses.

12.
In Python, what is the purpose of the __slots__ attribute in a class?
It specifies the names of class methods.
It defines the list of attributes that instances of the class can have.
It indicates the inheritance hierarchy of the class.
It is used to declare static variables in a class.
Answer: Option
Explanation:
The __slots__ attribute in a class is used to explicitly declare a list of allowed attributes for instances of the class. It can help in saving memory by preventing the creation of additional instance attributes.

13.
What is the purpose of the @abstractmethod decorator?
It creates an abstract class in Python.
It indicates a method that must be implemented by any concrete (non-abstract) subclass.
It is used to define a method that cannot be overridden.
It marks a method as private and inaccessible from outside the class.
Answer: Option
Explanation:
The @abstractmethod decorator is used to define abstract methods in an abstract class. Any concrete subclass must implement these abstract methods.

14.
What is the purpose of the isinstance() function?
It checks if a variable is an instance of a specified class.
It determines if an object has a particular attribute.
It checks if two objects are of the same type.
It verifies if an object is iterable.
Answer: Option
Explanation:
The isinstance() function is used to check if a variable is an instance of a specified class or a tuple of classes. It returns True if the object is an instance of any of the given classes.

15.
What is the primary purpose of the __doc__ attribute?
It stores the documentation string for a module, class, or function.
It is used to access the private documentation of a class.
It indicates the default values of the class attributes.
It defines the documentation for a class constructor.
Answer: Option
Explanation:
The __doc__ attribute in Python is used to access the documentation string (docstring) associated with a module, class, or function. It provides information about the purpose and usage of the code.