Exercise: Inheritance

Questions for: Inheritance

What is the purpose of the @property decorator?
A:
To define a class property
B:
To create a new instance of a class
C:
To access class attributes directly
D:
To override a method in the superclass
Answer: A
The @property decorator is used to define a property in a class, allowing the use of the property like an attribute.
What is the purpose of the __slots__ attribute in a Python class?
A:
To define class attributes
B:
To create a new instance of the class
C:
To restrict the attributes that can be dynamically added to instances
D:
To access superclass attributes directly
Answer: C
The __slots__ attribute restricts the attributes that can be dynamically added to instances, improving memory efficiency.
How does Python handle circular dependencies in the context of inheritance?
A:
It automatically resolves circular dependencies
B:
It raises an error, and the program crashes
C:
It requires explicit handling using additional syntax
D:
Circular dependencies are not allowed in Python
Answer: C
Python requires explicit handling of circular dependencies using techniques like late imports or rearranging code.
In Python, what is the purpose of the __init_subclass__() method?
A:
To initialize the attributes of a subclass
B:
To create a new instance of a subclass
C:
To initialize attributes common to all subclasses
D:
To customize the creation of subclasses
Answer: D
The __init_subclass__() method is called when a new subclass is created, allowing customization of the creation process.
What is the purpose of the __mro__ attribute?
A:
To access superclass attributes
B:
To check if an object is an instance of a specific class
C:
To determine the method resolution order of a class
D:
To create a new instance of the class
Answer: C
The __mro__ attribute is a tuple containing the method resolution order of a class, showing the order in which base classes are searched when resolving methods.
Ad Slot (Above Pagination)
Quiz