Exercise: Inheritance

Questions for: Inheritance

What is the purpose of the __next__() method in Python classes that implement iterators?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the next value when iterating over instances of the class
D:
To access superclass attributes directly
Answer: C
The __next__() method is used in classes that implement iterators to customize the next value when iterating over instances of the class.
How can you achieve encapsulation in Python without using private attributes?
A:
By using the @private decorator
B:
By using single leading underscores in attribute names
C:
Encapsulation is not possible without private attributes
D:
By using the @encapsulate decorator
Answer: B
Encapsulation in Python can be achieved by using a single leading underscore in attribute names, indicating that they are intended for internal use.
In Python, what is the purpose of the __hash__() method in a class?
A:
To define class attributes
B:
To create a new instance of the class
C:
To customize the hash value for instances of the class
D:
To access superclass attributes directly
Answer: C
The __hash__() method is used to customize the hash value for instances of a class when using them in hashable collections like dictionaries or sets.
What is the purpose of the __exit__() method in the context of Python's context managers?
A:
To create a new instance of the class
B:
To define class attributes
C:
To customize the exit behavior when exiting a with statement
D:
To access superclass attributes directly
Answer: C
The __exit__() method is used in context managers to customize the behavior when exiting a with statement, such as handling exceptions.
What is the purpose of the @abstractmethod decorator?
A:
To create a new instance of the class
B:
To define a method that must be implemented by subclasses
C:
To access class attributes directly
D:
To override a method in the superclass
Answer: B
The @abstractmethod decorator is used to define an abstract method in a class, which must be implemented by any concrete subclasses.
Ad Slot (Above Pagination)
Quiz