Python Programming - Classes
Exercise : Classes - General Questions
- Classes - General Questions
21.
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?
Answer: Option
Explanation:
The code demonstrates the concept of inheritance, where the `Dog` class inherits from the `Animal` class, acquiring its methods and attributes.
22.
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?
Answer: Option
Explanation:
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.
23.
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?
Answer: Option
Explanation:
`super().__init__(brand)` is used to call the constructor of the superclass (`Car` class) to initialize the `brand` attribute.
24.
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?
Answer: Option
Explanation:
The code demonstrates the concept of inheritance, where the `ColoredCircle` class inherits from the `Circle` class, acquiring its methods and attributes.
25.
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?
Answer: Option
Explanation:
The code illustrates the concept of inheritance, where the `SavingsAccount` class inherits from the `BankAccount` class, inheriting its attributes and methods.
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers