Exercise: Python

Questions for: Classes

Consider the following Python code:
class BankAccount:
    def __init__(self, balance):
        self.balance = balance

    def withdraw(self, amount):
        self.balance -= amount

class SavingsAccount(BankAccount):
    def __init__(self, balance, interest_rate):
        super().__init__(balance)
        self.interest_rate = interest_rate

    def add_interest(self):
        self.balance += self.balance * self.interest_rate
Which object-oriented principle is evident in the code?
A:
Polymorphism
B:
Encapsulation
C:
Inheritance
D:
Abstraction
Answer: C
The code illustrates the concept of inheritance, where the `SavingsAccount` class inherits from the `BankAccount` class, inheriting its attributes and methods.
Consider the following Python code:
class Circle:
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * self.radius**2

class ColoredCircle(Circle):
    def __init__(self, radius, color):
        super().__init__(radius)
        self.color = color
What concept of object-oriented programming is exemplified in this code?
A:
Encapsulation
B:
Polymorphism
C:
Abstraction
D:
Inheritance
Answer: D
The code demonstrates the concept of inheritance, where the `ColoredCircle` class inherits from the `Circle` class, acquiring its methods and attributes.
Consider the following Python code:
class Car:
    def __init__(self, brand):
        self.brand = brand

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

class Rectangle(Shape):
    def __init__(self, length, width):
        self.length = length
        self.width = width
    
    def area(self):
        return self.length * self.width
What principle of object-oriented programming is applied here?
A:
Abstraction
B:
Polymorphism
C:
Encapsulation
D:
Inheritance
Answer: D
The code illustrates the principle of inheritance, where the `Rectangle` class inherits from the `Shape` class and provides a specific implementation of the `area` method.
Consider the following Python code:
class Animal:
    def speak(self):
        return "Animal speaks"

class Dog(Animal):
    def speak(self):
        return "Dog barks"
What is the concept illustrated in this code?
A:
Encapsulation
B:
Inheritance
C:
Polymorphism
D:
Abstraction
Answer: B
The code demonstrates the concept of inheritance, where the `Dog` class inherits from the `Animal` class, acquiring its methods and attributes.
Ad Slot (Above Pagination)
Quiz