Exercise: Python

Questions for: Classes

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?
A:
Java Junction \n False
B:
False \n Java Junction
C:
Java Junction
D:
False
Answer: A
The code creates an instance of the `EspressoBar` class and prints the values of its attributes (`name`, `serves_alcohol`).
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?
A:
Eagle \n 2.5
B:
2.5 \n Eagle
C:
Eagle
D:
2.5
Answer: A
The code creates an instance of the `Bird` class and prints the values of its attributes (`species`, `wingspan`).
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?
A:
Alice \n 20 \n S12345
B:
S12345 \n Alice \n 20
C:
Alice \n S12345 \n 20
D:
20 \n Alice \n S12345
Answer: A
The code creates an instance of the `Student` class and prints the values of its attributes (`name`, `age`, `student_id`).
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?
A:
The electric car with 75 kWh battery is starting
B:
The Tesla engine is starting
C:
The electric car is starting
D:
The Tesla with 75 kWh battery engine is starting
Answer: A
The code creates an instance of the `ElectricCar` class, which overrides the `start_engine` method. The output reflects the overridden method in the subclass.
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?
A:
I am a Square \n My side length is 4
B:
My side length is 4 \n I am a Square
C:
I am a Square
D:
My side length is 4
Answer: A
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`).
Ad Slot (Above Pagination)
Quiz