← Back to Chapter 8
FINAL EXAM Chapter 8: OOP HARD
45:00
Problems
3
Time Limit
45 min
Solved
0/3
Score
0%
1
BankAccount Class
Not Started
2
Shape Hierarchy
Not Started
3
Employee System
Not Started

Problem 1: BankAccount Class

Difficulty: Hard | Topic: Class Design

Description

Create a complete BankAccount class with the following specifications.

Requirements

Constructor (__init__):

  • Parameters: owner (str), balance (float, default 0)
  • Store owner and balance as instance attributes
  • Create a transaction_history list, initially empty

Methods:

  • deposit(amount): Add amount to balance, record in history, return new balance
  • withdraw(amount): Subtract amount if sufficient funds, record in history, return new balance or -1 if insufficient
  • get_balance(): Return current balance
  • get_history(): Return list of transaction strings

Special Methods:

  • __str__: Return "Account: {owner}, Balance: ${balance}"

Transaction History Format

Each transaction should be recorded as:

  • Deposit: "Deposit: +${amount}"
  • Withdrawal: "Withdrawal: -${amount}"

Example

Usage:
acc = BankAccount("Alice", 100) acc.deposit(50) # Returns 150 acc.withdraw(30) # Returns 120 acc.withdraw(200) # Returns -1 (insufficient) print(acc) # Account: Alice, Balance: $120 acc.get_history() # ["Deposit: +$50", "Withdrawal: -$30"]
solution.py
Hint

Start by storing owner, balance, and an empty list for history. For withdraw, check if amount > self.balance first and return -1 if true. For __str__, use an f-string with the required format.

Output
Run your code to see output...

Problem 2: Shape Hierarchy

Difficulty: Hard | Topic: Inheritance & Polymorphism

Description

Create a shape class hierarchy with a base Shape class and two child classes: Rectangle and Circle.

Shape (Base Class)

  • __init__(self, name): Store the shape name
  • area(self): Return 0 (to be overridden)
  • perimeter(self): Return 0 (to be overridden)
  • describe(self): Return "{name}: Area={area}, Perimeter={perimeter}"

Rectangle (Child Class)

  • __init__(self, width, height): Call parent with "Rectangle", store width/height
  • area(self): Return width × height
  • perimeter(self): Return 2 × (width + height)

Circle (Child Class)

  • __init__(self, radius): Call parent with "Circle", store radius
  • area(self): Return π × radius² (use 3.14159)
  • perimeter(self): Return 2 × π × radius

Example

Usage:
rect = Rectangle(10, 5) rect.area() # 50 rect.perimeter() # 30 rect.describe() # "Rectangle: Area=50, Perimeter=30" circle = Circle(7) circle.area() # ~153.94 circle.perimeter() # ~43.98
solution.py
Hint

Use super().__init__("Rectangle") in child classes. The describe method in Shape should call self.area() and self.perimeter() - polymorphism will use the child's versions!

Output
Run your code to see output...

Problem 3: Employee Management System

Difficulty: Hard | Topic: Advanced OOP Design

Description

Create an employee management system with different employee types and a company class to manage them.

Employee (Base Class)

  • __init__(self, name, base_salary)
  • calculate_pay(self): Return base_salary
  • __str__(self): Return "{name} - ${pay}"

Manager (Child of Employee)

  • __init__(self, name, base_salary, bonus)
  • calculate_pay(self): Return base_salary + bonus

Developer (Child of Employee)

  • __init__(self, name, base_salary, projects): projects is int
  • calculate_pay(self): Return base_salary + (projects × 500)

Company

  • __init__(self, name): Store name, create empty employees list
  • add_employee(self, employee): Add to list
  • total_payroll(self): Sum of all employee pay
  • get_employees(self): Return list of employee names

Example

Usage:
company = Company("TechCorp") company.add_employee(Employee("Alice", 50000)) company.add_employee(Manager("Bob", 60000, 10000)) company.add_employee(Developer("Charlie", 55000, 3)) company.total_payroll() # 126500 company.get_employees() # ["Alice", "Bob", "Charlie"]
solution.py
Hint

Use super().__init__(name, base_salary) in Manager and Developer. For Company, use sum(emp.calculate_pay() for emp in self.employees) for total_payroll. Use a list comprehension for get_employees.

Output
Run your code to see output...