Python Programming - Classes
Exercise : Classes - General Questions
- Classes - General Questions
31.
Consider the following Python code:
class Animal:
def __init__(self, species):
self.species = species
class Dog(Animal):
def __init__(self, species, breed):
super().__init__(species)
self.breed = breed
# Create an instance of the Dog class
my_dog = Dog("Canine", "Labrador")
# Access and print attributes
print(my_dog.species)
print(my_dog.breed)
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Dog` class, initializes its attributes, and then prints the values of the `species` and `breed` attributes.
32.
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 Circle(Shape):
def __init__(self, name, radius):
super().__init__(name)
self.radius = radius
def display_info(self):
super().display_info()
print(f"My radius is {self.radius}")
# Create an instance of the Circle class
my_circle = Circle("Circle", 5)
# Call the display_info method
my_circle.display_info()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Circle` class, which overrides the `display_info` method. The output includes messages from both the superclass (`Shape`) and the subclass (`Circle`).
33.
Consider the following Python code:
class Car:
def __init__(self, brand, model):
self.brand = brand
self.model = model
def display_info(self):
print(f"{self.brand} {self.model}")
class ElectricCar(Car):
def __init__(self, brand, model, battery_capacity):
super().__init__(brand, model)
self.battery_capacity = battery_capacity
def display_info(self):
super().display_info()
print(f"Battery Capacity: {self.battery_capacity} kWh")
# Create an instance of the ElectricCar class
my_electric_car = ElectricCar("Tesla", "Model S", 100)
# Call the display_info method
my_electric_car.display_info()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `ElectricCar` class, which overrides the `display_info` method. The output includes messages from both the superclass (`Car`) and the subclass (`ElectricCar`).
34.
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
# Create an instance of the EBook class
my_ebook = EBook("Python Basics", "John Doe", "PDF")
# Access and print attributes
print(my_ebook.title)
print(my_ebook.author)
print(my_ebook.format_type)
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `EBook` class and prints the values of its attributes (`title`, `author`, `format_type`).
35.
Consider the following Python code:
class Animal:
def __init__(self, species):
self.species = species
class Fish(Animal):
def swim(self):
print(f"{self.species} is swimming")
# Create an instance of the Fish class
my_fish = Fish("Goldfish")
# Call the swim method
my_fish.swim()
What will be the output of the code?
Answer: Option
Explanation:
The code creates an instance of the `Fish` class and calls the `swim` method, which prints the message "{species} is swimming."
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers