Exercise: Classes
Questions for: Classes
What is the primary purpose of the
__doc__ attribute?
A:
It stores the documentation string for a module, class, or function.
B:
It is used to access the private documentation of a class.
C:
It indicates the default values of the class attributes.
D:
It defines the documentation for a class constructor.
Answer: A
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.
What is the purpose of the
isinstance() function?
A:
It checks if a variable is an instance of a specified class.
B:
It determines if an object has a particular attribute.
C:
It checks if two objects are of the same type.
D:
It verifies if an object is iterable.
Answer: A
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.Discuss About this Question.
What is the purpose of the
@abstractmethod decorator?
A:
It creates an abstract class in Python.
B:
It indicates a method that must be implemented by any concrete (non-abstract) subclass.
C:
It is used to define a method that cannot be overridden.
D:
It marks a method as private and inaccessible from outside the class.
Answer: B
The
@abstractmethod decorator is used to define abstract methods in an abstract class. Any concrete subclass must implement these abstract methods.Discuss About this Question.
In Python, what is the purpose of the
__slots__ attribute in a class?
A:
It specifies the names of class methods.
B:
It defines the list of attributes that instances of the class can have.
C:
It indicates the inheritance hierarchy of the class.
D:
It is used to declare static variables in a class.
Answer: B
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.Discuss About this Question.
What is the purpose of the
@property decorator?
A:
It defines a property of a class that can be accessed using dot notation.
B:
It marks a method as a getter for a class attribute.
C:
It is used to create an instance variable in a class.
D:
It indicates a method that can only be accessed by the class itself.
Answer: B
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.Discuss About this Question.
Ad Slot (Above Pagination)
Discuss About this Question.