Python Programming - Classes

Exercise : Classes - General Questions
  • Classes - General Questions
26.
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?
Polymorphism
Encapsulation
Inheritance
Abstraction
Answer: Option
Explanation:
The code illustrates the concept of inheritance, where the `Car` class inherits from the `Vehicle` class, inheriting its attributes and methods.

27.
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?
Encapsulation
Inheritance
Polymorphism
Abstraction
Answer: Option
Explanation:
The code demonstrates the concept of inheritance, where the `Manager` class inherits from the `Employee` class, inheriting its attributes and methods.

28.
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?
Abstraction
Polymorphism
Encapsulation
Inheritance
Answer: Option
Explanation:
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.

29.
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?
It initializes the color attribute of the Apple class.
It defines the constructor for the Apple class.
It creates an instance of the Fruit class.
It calls the constructor of the superclass to set the color attribute.
Answer: Option
Explanation:
`super().__init__(color)` is used to call the constructor of the superclass (`Fruit` class) to initialize the `color` attribute of the `Apple` class.

30.
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?
Polymorphism
Encapsulation
Inheritance
Abstraction
Answer: Option
Explanation:
The code illustrates the concept of inheritance, where the `EBook` class inherits from the `Book` class, inheriting its attributes and methods.