Python Programming - Classes
Exercise : Classes - General Questions
- Classes - General Questions
36.
Consider the following Python code:
class Shape:
def __init__(self, name):
self.name = name
def display_info(self):
print(f"I am a {self.name}")
class Square(Shape):
def __init__(self, name, side_length):
super().__init__(name)
self.side_length = side_length
def display_info(self):
super().display_info()
print(f"My side length is {self.side_length}")
# Create an instance of the Square class
my_square = Square("Square", 4)
# Call the display_info method
my_square.display_info()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Square` class, which overrides the `display_info` method. The output includes messages from both the superclass (`Shape`) and the subclass (`Square`).
37.
Consider the following Python code:
class Vehicle:
def __init__(self, brand):
self.brand = brand
def start_engine(self):
print(f"The {self.brand} engine is starting")
class ElectricCar(Vehicle):
def __init__(self, brand, battery_capacity):
super().__init__(brand)
self.battery_capacity = battery_capacity
def start_engine(self):
print(f"The electric car with {self.battery_capacity} kWh battery is starting")
# Create an instance of the ElectricCar class
my_electric_car = ElectricCar("Tesla", 75)
# Call the start_engine method
my_electric_car.start_engine()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `ElectricCar` class, which overrides the `start_engine` method. The output reflects the overridden method in the subclass.
38.
Consider the following Python code:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
class Student(Person):
def __init__(self, name, age, student_id):
super().__init__(name, age)
self.student_id = student_id
# Create an instance of the Student class
my_student = Student("Alice", 20, "S12345")
# Access and print attributes
print(my_student.name)
print(my_student.age)
print(my_student.student_id)
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Student` class and prints the values of its attributes (`name`, `age`, `student_id`).
39.
Consider the following Python code:
class Animal:
def __init__(self, species):
self.species = species
class Bird(Animal):
def __init__(self, species, wingspan):
super().__init__(species)
self.wingspan = wingspan
# Create an instance of the Bird class
my_bird = Bird("Eagle", 2.5)
# Access and print attributes
print(my_bird.species)
print(my_bird.wingspan)
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Bird` class and prints the values of its attributes (`species`, `wingspan`).
40.
Consider the following Python code:
class CoffeeShop:
def __init__(self, name):
self.name = name
class EspressoBar(CoffeeShop):
def __init__(self, name, serves_alcohol):
super().__init__(name)
self.serves_alcohol = serves_alcohol
# Create an instance of the EspressoBar class
my_espresso_bar = EspressoBar("Java Junction", False)
# Access and print attributes
print(my_espresso_bar.name)
print(my_espresso_bar.serves_alcohol)
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `EspressoBar` class and prints the values of its attributes (`name`, `serves_alcohol`).
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers