Exercise: Python

Questions for: Classes

Consider the following Python 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 object-oriented principle is evident in this code?
A:
Polymorphism
B:
Encapsulation
C:
Inheritance
D:
Abstraction
Answer: C
The code illustrates the concept of inheritance, where the `EBook` class inherits from the `Book` class, inheriting its attributes and methods.
Consider the following Python code:
class Fruit:
    def __init__(self, color):
        self.color = color

class Apple(Fruit):
    def __init__(self, color, variety):
        super().__init__(color)
        self.variety = variety
What is the purpose of `super().__init__(color)` in the `Apple` class?
A:
It initializes the color attribute of the Apple class.
B:
It defines the constructor for the Apple class.
C:
It creates an instance of the Fruit class.
D:
It calls the constructor of the superclass to set the color attribute.
Answer: D
`super().__init__(color)` is used to call the constructor of the superclass (`Fruit` class) to initialize the `color` attribute of the `Apple` class.
Consider the following Python code:
class Shape:
    def area(self):
        pass

class Triangle(Shape):
    def __init__(self, base, height):
        self.base = base
        self.height = height
    
    def area(self):
        return 0.5 * self.base * self.height
What principle of object-oriented programming is applied here?
A:
Abstraction
B:
Polymorphism
C:
Encapsulation
D:
Inheritance
Answer: D
The code exemplifies the principle of inheritance, where the `Triangle` class inherits from the `Shape` class and provides a specific implementation of the `area` method.
Consider the following Python code:
class Employee:
    def __init__(self, name, salary):
        self.name = name
        self.salary = salary

    def display_info(self):
        return f"{self.name} earns {self.salary} per month."

class Manager(Employee):
    def __init__(self, name, salary, team_size):
        super().__init__(name, salary)
        self.team_size = team_size
Which object-oriented principle is applied in this code?
A:
Encapsulation
B:
Inheritance
C:
Polymorphism
D:
Abstraction
Answer: B
The code demonstrates the concept of inheritance, where the `Manager` class inherits from the `Employee` class, inheriting its attributes and methods.
Consider the following Python code:
class Vehicle:
    def __init__(self, brand):
        self.brand = brand

class Car(Vehicle):
    def __init__(self, brand, model):
        super().__init__(brand)
        self.model = model
What concept of object-oriented programming is demonstrated in this code?
A:
Polymorphism
B:
Encapsulation
C:
Inheritance
D:
Abstraction
Answer: C
The code illustrates the concept of inheritance, where the `Car` class inherits from the `Vehicle` class, inheriting its attributes and methods.
Ad Slot (Above Pagination)
Quiz