Exercise: Python

Questions for: Inheritance

What is the purpose of the __doc__ attribute in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To provide documentation for the class
D:
To access superclass attributes directly
Answer: C
The __doc__ attribute is used to provide documentation (docstring) for a class, describing its purpose and usage.
How does Python support method overloading?
A:
By allowing multiple methods with the same name but different parameters
B:
By using the @overload decorator
C:
By explicitly specifying the data type of method parameters
D:
Method overloading is not supported in Python
Answer: D
Python does not support method overloading in the traditional sense, as methods with the same name in a class will override each other.
In Python, what is the purpose of the @property decorator for methods?
A:
To create a new instance of the class
B:
To define a method that can be accessed like an attribute
C:
To access class attributes directly
D:
To override a method in the superclass
Answer: B
The @property decorator is used to define a method that can be accessed like an attribute, allowing it to be called without parentheses.
What is the purpose of the __setattr__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the behavior when setting an attribute on an instance
D:
To access superclass attributes directly
Answer: C
The __setattr__() method is used to customize the behavior when setting an attribute on an instance of a class.
What is the purpose of the __repr__() method in Python classes?
A:
To define class attributes
B:
To create a new instance of the class
C:
To provide a string representation of the object for debugging
D:
To access superclass attributes directly
Answer: C
The __repr__() method is used to provide a string representation of the object for debugging and inspection purposes.
Ad Slot (Above Pagination)
Quiz