Exercise: Python

Questions for: Inheritance

Consider the following code:
class Bird:
    def __init__(self, species):
        self.species = species

class Parrot(Bird):
    def __init__(self, species, color):
        super().__init__(species)
        self.color = color
What is the primary purpose of using super().__init__(species) in the Parrot class constructor?
A:
To call the constructor of the Parrot class
B:
To create a new instance of the Bird class
C:
To initialize attributes of the Bird class in the Parrot class
D:
To access superclass attributes directly
Answer: C
super().__init__(species) is used to call the constructor of the superclass Bird and initialize the species attribute.
In Python, what is the purpose of the __str__() method in a class?
A:
To define class attributes
B:
To create a new instance of the class
C:
To represent the object as a string for debugging
D:
To provide a human-readable string representation of the object
Answer: D
The __str__() method is used to provide a human-readable string representation of the object.
Consider the following code:
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

class EBook(Book):
    def __init__(self, title, author, format_type):
        super().__init__(title, author)
        self.format_type = format_type
What is the primary advantage of using super().__init__(title, author) in the EBook class constructor?
A:
To call the constructor of the EBook class
B:
To create a new instance of the Book class
C:
To initialize attributes of the Book class in the EBook class
D:
To access superclass attributes directly
Answer: C
super().__init__(title, author) is used to call the constructor of the superclass Book and initialize the title and author attributes.
Consider the following code:
class Shape:
    def __init__(self, color):
        self.color = color

class Square(Shape):
    def __init__(self, color, side_length):
        super().__init__(color)
        self.side_length = side_length
What is the primary advantage of using super().__init__(color) in the Square class constructor?
A:
To call the constructor of the Square class
B:
To create a new instance of the Shape class
C:
To initialize attributes of the Shape class in the Square class
D:
To access superclass attributes directly
Answer: C
super().__init__(color) is used to call the constructor of the superclass Shape and initialize the color attribute.
In Python, what is the purpose of the issubclass() function?
A:
To check if an object is an instance of a class
B:
To check if a class is a subclass of another class
C:
To check if two classes have the same attributes
D:
To check if an object has a specific attribute
Answer: B
The issubclass() function is used to check if a class is a subclass of another class.
Ad Slot (Above Pagination)
Quiz