Define a class, EBook, that inherits from Book.
In the EBook class, override the read method so that it increases current_page by pages_read * 2.
Create an instance of EBook, call its read method.
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 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: |
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: |
class Student:
pass
class University:
pass
def register(uni, data):
for grade, names in data.items():
if not names:
continue
for name in names:
st = Student()
st.name, st.grade = name, grade
uni.students.append(st)
def get_student(university, grade):
for st in university.students:
if st.grade == grade:
return st
data = {8: ['Ariana', 'Dafina'], 9: ['Besnik'], 7: []}
university = University()
university.name, university.students = "Ise Boletini", []
register(university, data)
first = get_student(university, 9)
second = get_student(university, 5)
At the end of execution of the above script, what is value of?
| first.name: | ||
| second: |
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.
Complete execution flow of the following program
class Fighter: def __init__(self, name, strength, moves): self.name = name self.strength = strength self.moves = moves self.health = 100 def train(self, new_move): self.moves.append(new_move) def punch(self, opponent): opponent.health = opponent.health - 5 def grapple(self, opponent): opponent.health = opponent.health - 5 class Boxer(Fighter): def punch(self, opponent): opponent.health = opponent.health - 10 class Wrestler(Fighter): def grapple(self, opponent): opponent.health = opponent.health - 10 fighter = Fighter("Adesanya", 70, ["feint + counter right", "left hook"]) boxer = Boxer("Rocky", 80, ["jab", "cross"]) wrestler = Wrestler("Hulk", 90, ["slam", "suplex", "chokehold"]) fighter.punch(boxer) boxer.punch(wrestler) wrestler.punch(fighter)
Complete execution flow of the following program
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 v4.liters
add div >>>
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)
class Fighter:
def __init__(self, name, strength, moves):
self.name = name
self.strength = strength
self.moves = moves
self.health = 100
def train(self, new_move):
self.moves.append(new_move)
class Boxer(Fighter):
def punch(self, opponent):
opponent.health -= 10
class Wrestler(Fighter):
def grapple(self, opponent):
opponent.health -= 15
boxer = Boxer("Rocky", 80, ["jab", "cross"])
wrestler = Wrestler("Hulk", 90, ["slam", "suplex", "chokehold"])
boxer.train("uppercut")
wrestler.grapple(boxer)
Given the above script, what is value of the following expressions?
| boxer.moves[-1]: | ||
| boxer.health: | ||
| wrestler.moves[0]: | ||
| wrestler.strength: |