Exercise: Python

Questions for: Classes

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?
A:
Goldfish is swimming
B:
Fish is swimming
C:
Goldfish
D:
Goldfish is moving
Answer: A
The code creates an instance of the `Fish` class and calls the `swim` method, which prints the message "{species} is swimming."
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?
A:
Python Basics \n John Doe \n PDF
B:
PDF \n Python Basics \n John Doe
C:
Python Basics \n PDF \n John Doe
D:
John Doe \n Python Basics \n PDF
Answer: A
The code creates an instance of the `EBook` class and prints the values of its attributes (`title`, `author`, `format_type`).
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?
A:
Tesla Model S \n Battery Capacity: 100 kWh
B:
Battery Capacity: 100 kWh \n Tesla Model S
C:
Tesla Model S
D:
Battery Capacity: 100 kWh
Answer: A
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`).
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?
A:
I am a Circle \n My radius is 5
B:
My radius is 5 \n I am a Circle
C:
I am a Circle
D:
My radius is 5
Answer: A
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`).
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?
A:
Canine \n Labrador
B:
Labrador \n Canine
C:
Canine
D:
Labrador
Answer: A
The code creates an instance of the `Dog` class, initializes its attributes, and then prints the values of the `species` and `breed` attributes.
Ad Slot (Above Pagination)
Quiz