Define a class, Smartphone, that inherits from Device.
In the Smartphone class, override the use method so that reduces its battery by hours * 7 and adds hours to usage_hours.
Create an instance of Smartphone, call its use method.
Define a class, Car, that inherits from Vehicle.
In the Car class, override the __str__ method so that it returns a string with car's brand and mileage.
In the Car class, override the refuel method so that it increases the car's fuel by liters * 1.2
Create an instance of Car, call its refuel method, and print the car.
class Volume:
def __init__(self, liters):
self.liters = liters
def __add__(self, other):
print('add')
return Volume(self.liters + other.liters)
def __truediv__(self, divisor):
print('div')
return Volume(self.liters / divisor)
v1 = Volume(10)
v2 = Volume(5)
v3 = v1 + v2
v4 = v1 / 2
Given the above script:
| What is the first printed value: | ||
| What is the second printed value: | ||
| What is value of v1.liters: | ||
| What is value of v3.liters: | ||
| What is value of v4.liters: |
Define a class, Car, that inherits from Vehicle.
In the Car class, override the drive method so that it decreases the car's fuel by distance * 0.05 and adds distance to mileage
Create an instance of Car, call its drive and refuel methods, and observe the fuel change.
class Employee:
def __init__(self, name, salary, projects):
self.name = name
self.salary = salary
self.projects = projects
self.hours_worked = 0
def add_project(self, project):
self.projects.append(project)
def work(self, hours):
self.hours_worked += hours
def get_bonus(self):
return self.salary * 0.1
class Developer(Employee):
def work(self, hours):
self.hours_worked += hours * 1.5
class Manager(Employee):
def get_bonus(self):
return self.salary * 0.2
employee = Employee("John", 50000, ["Project A"])
developer = Developer("Alice", 70000, ["Project B", "Project C"])
manager = Manager("Bob", 90000, ["Project D"])
employee.work(10)
developer.work(10)
manager.work(10)
At the end of execution of the above script, what is value of the following expressions?
| employee.hours_worked: | ||
| developer.hours_worked: | ||
| manager.hours_worked: |
Define a class, Goat, that inherits from Animal.
In the Goat class, override the __str__ method so that it returns a string with goat's name and energy.
In the Goat class, override the play method so that it decreases the goat's energy by hours * 5
Create an instance of Goat, call its play method, and print the goat.
Complete execution flow of the following program
class Money: def set_info(self, currency, amount): self.currency = currency self.amount = amount def add(self, other): new_money = Money() new_money.set_info(self.currency, self.amount + other.amount) return new_money def subtract(self, other): new_money = Money() new_money.set_info(self.currency, self.amount - other.amount) return new_money def multiply(self, factor): new_money = Money() new_money.set_info(self.currency, self.amount * factor) return new_money def divide(self, divisor): new_money = Money() new_money.set_info(self.currency, self.amount / divisor) return new_money wallet1 = Money() wallet1.set_info("EUR", 200) wallet2 = Money() wallet2.set_info("EUR", 100) total = wallet1.add(wallet2) remaining = total.multiply(2) total.amount, remaining.amount
Complete execution flow of the following program
class Employee: def __init__(self, name, salary, projects): self.name = name self.salary = salary self.projects = projects self.hours_worked = 0 def add_project(self, project): self.projects.append(project) def work(self, hours): self.hours_worked = self.hours_worked + hours def get_bonus(self): return self.salary * 0.1 class Developer(Employee): def work(self, hours): self.hours_worked = self.hours_worked + hours * 1.5 class Manager(Employee): def get_bonus(self): return self.salary * 0.2 employee = Employee("John", 50000, ["Project A"]) developer = Developer("Alice", 70000, ["Project B", "Project C"]) manager = Manager("Bob", 90000, ["Project D"]) employee.work(10) developer.work(10) manager.work(10)
Define a class, Goat, that inherits from Animal.
In the Goat class, override the sleep method so that it increases the goat's energy by hours * 20
Create an instance of Goat, call its play and sleep methods, and observe the energy change.
class Student:
def __init__(self, name, grade, credits):
self.name = name
self.grade = grade
self.credits = credits
self.courses = []
def enroll(self, course):
self.courses.append(course)
class GraduateStudent(Student):
def research(self, hours):
self.credits += hours * 2
class Undergraduate(Student):
def study(self, hours):
self.credits += hours
grad = GraduateStudent("Alice", 90, 30)
undergrad = Undergraduate("Bob", 85, 20)
grad.enroll("ML")
grad.research(5)
undergrad.study(10)
Given the above script, what is value of the following expressions?
| grad.courses[0]: | ||
| grad.credits: | ||
| undergrad.credits: | ||
| undergrad.grade: | ||
| undergrad.courses: |